Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request admin privileges for Java app on Windows Vista

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?

like image 748
dkp Avatar asked Nov 03 '08 14:11

dkp


People also ask

How do I give Java admin rights?

Run your command prompt in administrator mode. To more specific: Locate the %windir%\system32\cmd.exe file. Right click and run as administrator privilege.

Do I need admin rights to install Java?

You cannot install the JDK provided by Oracle without administrator right.


2 Answers

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.

like image 100
freecouch Avatar answered Sep 23 '22 20:09

freecouch


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

like image 38
James Van Huis Avatar answered Sep 23 '22 20:09

James Van Huis