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?
Run your command prompt in administrator mode. To more specific: Locate the %windir%\system32\cmd.exe file. Right click and run as administrator privilege.
You cannot install the JDK provided by Oracle without administrator right.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With