Question

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.

When I use the "Run as adminstrator" option it runs on Vista as well..

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?

Was it helpful?

Solution

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

OTHER TIPS

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"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top