Request admin privileges for Java app on Windows Vista

╄→гoц情女王★ 提交于 2019-11-28 08:38:17

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

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.

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.

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.

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.

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

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

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.

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

Using the launch4j Gradle plugin

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

As in freecouch' answer, create a file 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

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:

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"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!