在 Windows Vista 上请求 Java 应用程序的管理员权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/258728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request admin privileges for Java app on Windows Vista
提问by dkp
When I try to create a new task in the task scheduler via the Java ProcessBuilder class I get an access denied error an Windows Vista. On XP it works just fine.
当我尝试通过 Java ProcessBuilder 类在任务调度程序中创建新任务时,我收到 Windows Vista 拒绝访问错误。在 XP 上它工作得很好。
When I use the "Run as adminstrator" option it runs on Vista as well..
当我使用“以管理员身份运行”选项时,它也可以在 Vista 上运行。
However this is a additional step requeried an the users might not know about this. When the user just double clicks on the app icon it will fail with access denied. My question is how can I force a java app to reuest admin privileges right after startup?
然而,这是一个额外的步骤,用户可能不知道这一点。当用户双击应用程序图标时,它会因访问被拒绝而失败。我的问题是如何强制 Java 应用程序在启动后立即重新获得管理员权限?
采纳答案by James Van Huis
I'm not sure you can do it programmatically. If you have an installer for your app, you can add the registry key to force run as admin:
我不确定您是否可以以编程方式进行。如果您的应用程序有安装程序,则可以添加注册表项以强制以管理员身份运行:
Path: HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Currentversion\Appcompatflags\layers
Key: << Full path to exe >>
Value: RUNASADMIN
Type: REG_SZ
路径:HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Currentversion\Appcompatflags\layers
键:<<exe 的完整路径>>
值:RUNASADMIN
类型:REG_SZ
回答by freecouch
Have you considered wrapping your Java application in an .exe using launch4j? By doing this you can embed a manifest file that allows you to specify the "execution level" for your executable. In other words, you control the privileges that should be granted to your running application, effectively telling the OS to "Run as adminstrator" without requiring the user to manually do so.
您是否考虑过使用 launch4j 将 Java 应用程序包装在 .exe 中?通过这样做,您可以嵌入一个清单文件,该文件允许您为可执行文件指定“执行级别”。换句话说,您可以控制应授予正在运行的应用程序的权限,从而有效地告诉操作系统“以管理员身份运行”,而无需用户手动执行此操作。
For example...
例如...
build.xml:
构建.xml:
<target name="wrapMyApp" depends="myapp.jar">
<launch4j configFile="myappConfig.xml" />
</target>
myappConfig.xml:
myappConfig.xml:
<launch4jConfig>
<dontWrapJar>false</dontWrapJar>
<headerType>gui</headerType>
<jar>bin\myapp.jar</jar>
<outfile>bin\myapp.exe</outfile>
<priority>normal</priority>
<downloadUrl>http://java.com/download</downloadUrl>
<customProcName>true</customProcName>
<stayAlive>false</stayAlive>
<manifest>myapp.manifest</manifest>
<jre>
<path></path>
<minVersion>1.5.0</minVersion>
<maxVersion></maxVersion>
<jdkPreference>preferJre</jdkPreference>
</jre>
</launch4jConfig>
myapp.manifest:
myapp.manifest:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="highestAvailable"
uiAccess="False" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
See http://msdn.microsoft.com/en-us/library/bb756929.aspxand the launch4j website for mode details.
有关模式详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb756929.aspx和 launch4j 网站。
回答by Airy
Though FreeCouch's answer is good but I faced some problem and thought of putting it here so next time others can be benefited easily.
虽然 FreeCouch 的回答很好,但我遇到了一些问题,并想把它放在这里,以便下次其他人可以轻松受益。
To get Administrator Permission in Java, as far as I found the solution is to wrap the *.jar inside a *.exe with program like Launch4J and binding Manifest with it.
要在 Java 中获得管理员权限,据我所知,解决方案是将 *.jar 包装在 *.exe 中,并使用 Launch4J 之类的程序并将 Manifest 与其绑定。
To create a Manifest file, Open any text editor like Notepad and paste these lines in it and save it with the file name along with its extension for e.g: myApplication.exe.manifest
要创建 Manifest 文件,请打开任何文本编辑器(如记事本)并将这些行粘贴到其中,并将其与文件名及其扩展名一起保存,例如:myApplication.exe.manifest
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="highestAvailable" uiAccess="False" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
In Launch4J, on Basictab add this file in Wrapper Manifest option.
在 Launch4J 中,在Basic选项卡上的 Wrapper Manifest 选项中添加此文件。
回答by PHPirate
Using the launch4j Gradle plugin
使用 launch4j Gradle 插件
Especially when already using Gradle, it is easy to apply the launch4j pluginto fix this.
特别是当已经使用 Gradle 时,很容易应用launch4j 插件来解决这个问题。
As in freecouch' answer, create a file myapp.manifestwith
作为freecouch的回答,创建一个文件myapp.manifest与
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="highestAvailable"
uiAccess="False" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
then apply the Gradle plugin by adding to your build.gradle
然后通过添加到您的 build.gradle
plugins {
id 'java'
id 'edu.sc.seis.launch4j' version '2.4.3'
}
launch4j {
mainClassName = 'com.mypackage.MainClass'
icon = "$projectDir/icons/icon.ico"
manifest = "$projectDir/myapp.manifest"
}
or, if using Kotlin Gradle DSL, to your build.gradle.kts:
或者,如果使用 Kotlin Gradle DSL,到您的build.gradle.kts:
plugins {
application
kotlin("jvm") version "1.2.31"
java
id("edu.sc.seis.launch4j") version "2.4.3"
}
launch4j {
mainClassName = "com.mypackage.MainKt"
icon = "$projectDir/icons/icon.ico"
manifest = "$projectDir/myapp.manifest"
}
回答by Phung D. An
I haven't found the best solution yet. But I have an alternative work-around comparing to "James Van Huis". Use RUNASINVOKE instead so you don't have to see the prompt Allow/Deny everytime you run the app.
我还没有找到最好的解决方案。但与“James Van Huis”相比,我有另一种解决方法。请改用 RUNASINVOKE,这样您就不必在每次运行应用程序时看到提示允许/拒绝。
Note: you always have to be Admin, that what I am trying to solve
注意:您必须始终是管理员,这是我要解决的问题

