在Windows Vista上请求Java应用程序的管理员权限

时间:2023-01-19 14:33:26

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..

当我使用“Run as adminstrator”选项时,它也可以在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应用程序在启动后立即重新获得管理员权限?

5 个解决方案

#1


3  

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:

我不确定你能以编程方式做到这一点。如果您的应用程序有安装程序,则可以添加注册表项以强制运行为admin:

Path: HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Currentversion\Appcompatflags\layers

路径:HKEY_CURRENT_USER \ Software \ Microsoft \ Windows NT \ Currentversion \ Appcompatflags \ layers

Key: << Full path to exe >>

关键:<< exe的完整路径>>

Value: RUNASADMIN

Type: REG_SZ

#2


24  

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在.exe中包装Java应用程序?通过执行此操作,您可以嵌入清单文件,该文件允许您指定可执行文件的“执行级别”。换句话说,您可以控制应该授予正在运行的应用程序的权限,从而有效地告诉操作系统“以管理员身份运行”,而无需用户手动执行此操作。

For example...

build.xml:

<target name="wrapMyApp" depends="myapp.jar">
        <launch4j configFile="myappConfig.xml" />
</target>

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:

<?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.aspx and the launch4j website for mode details.

有关模式详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb756929.aspx和launch4j网站。

#3


2  

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中的管理员权限,据我所知,解决方案是使用Launch4J等程序将* .jar包装在* .exe中并使用它绑定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

要创建清单文件,请打开任何文本编辑器(如记事本)并在其中粘贴这些行,并使用文件名及其扩展名保存它,例如: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 Basic tab add this file in Wrapper Manifest option.

在Launch4J中,在Basic选项卡上,在Wrapper Manifest选项中添加此文件。

#4


0  

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.

我还没有找到最好的解决方案。但我有一个替代工作 - 与“詹姆斯范惠斯”相比。请改用RUNASINVOKE,这样您每次运行应用程序时都不必看到提示允许/拒绝。

Note: you always have to be Admin, that what I am trying to solve

注意:你总是必须是管理员,这是我想要解决的问题

#5


0  

Using the launch4j Gradle plugin

使用launch4j Gradle插件

Especially when already using Gradle, it is easy to apply the launch4j plugin to fix this.

特别是当已经使用Gradle时,很容易应用launch4j插件来解决这个问题。

As in freecouch' answer, create a file myapp.manifest with

在freecouch的回答中,创建一个文件myapp.manifest with

<?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来应用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"
}

#1


3  

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:

我不确定你能以编程方式做到这一点。如果您的应用程序有安装程序,则可以添加注册表项以强制运行为admin:

Path: HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Currentversion\Appcompatflags\layers

路径:HKEY_CURRENT_USER \ Software \ Microsoft \ Windows NT \ Currentversion \ Appcompatflags \ layers

Key: << Full path to exe >>

关键:<< exe的完整路径>>

Value: RUNASADMIN

Type: REG_SZ

#2


24  

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在.exe中包装Java应用程序?通过执行此操作,您可以嵌入清单文件,该文件允许您指定可执行文件的“执行级别”。换句话说,您可以控制应该授予正在运行的应用程序的权限,从而有效地告诉操作系统“以管理员身份运行”,而无需用户手动执行此操作。

For example...

build.xml:

<target name="wrapMyApp" depends="myapp.jar">
        <launch4j configFile="myappConfig.xml" />
</target>

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:

<?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.aspx and the launch4j website for mode details.

有关模式详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb756929.aspx和launch4j网站。

#3


2  

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中的管理员权限,据我所知,解决方案是使用Launch4J等程序将* .jar包装在* .exe中并使用它绑定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

要创建清单文件,请打开任何文本编辑器(如记事本)并在其中粘贴这些行,并使用文件名及其扩展名保存它,例如: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 Basic tab add this file in Wrapper Manifest option.

在Launch4J中,在Basic选项卡上,在Wrapper Manifest选项中添加此文件。

#4


0  

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.

我还没有找到最好的解决方案。但我有一个替代工作 - 与“詹姆斯范惠斯”相比。请改用RUNASINVOKE,这样您每次运行应用程序时都不必看到提示允许/拒绝。

Note: you always have to be Admin, that what I am trying to solve

注意:你总是必须是管理员,这是我想要解决的问题

#5


0  

Using the launch4j Gradle plugin

使用launch4j Gradle插件

Especially when already using Gradle, it is easy to apply the launch4j plugin to fix this.

特别是当已经使用Gradle时,很容易应用launch4j插件来解决这个问题。

As in freecouch' answer, create a file myapp.manifest with

在freecouch的回答中,创建一个文件myapp.manifest with

<?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来应用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"
}