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?


Asked by: Rubie814 | Posted: 28-01-2022






Answer 1

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.

Answered by: Abigail520 | Posted: 01-03-2022



Answer 2

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

Answered by: Sienna519 | Posted: 01-03-2022



Answer 3

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.

Answered by: Kristian574 | Posted: 01-03-2022



Answer 4

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

Answered by: Wilson178 | Posted: 01-03-2022



Answer 5

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"
}

Answered by: Blake534 | Posted: 01-03-2022



Similar questions

java - Internal HSQL database complains about privileges

I'm setting up a standalone Java service with an in-process, in-memory HSQL database. Persistence.xml &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"&gt; &lt;persistence-unit nam...


java - Using Keyboards in JInput without root privileges

I'm writing a program that needs to poll keyboard keys at specific times, and rather than going through the effort of writing my own event-driven keyboard polling class, I thought I'd use JInput's built-in Keyboard class. It works perfectly when I run my program as root (I'm running on Ubuntu 10.10) but it doesn't even detect the fact that the keyboard exists when run as a normal user. I get the following error o...


Java Web Start of JRE 1.6.0_13 is not working on XP for user who does not have Admin privileges

Here I need to upgrade my Java Web start from JRE 5x to 6x so that my application can work on Windows7 as JRE 5x Java Web start does not work on Windows7. Java Web Start of JRE 1.6.0_13 is not working on XP for user who does not have Admin privileges. It works properly if user have admin privileges. In JNLP: Java Web Start of JRE 1.5.0_11 works fine and download the required JRE from my server irr...


How to launch an app with elevated privileges on Vista+ using Java / JNA to trigger UAC

I'd like to programmatically launch an application (a second JVM) from a Java app using JNA. Please note that I can't : add a Windows manifest to the JVM (minimal impact on the client) use right click "runas administrator" (clicking "Allow" in the UAC dialog will be complicated enough) I've seen other Questions with answers that states "use an exe" like


unix - Fork and drop privileges with Java

I'm writing a server program in Java that will allow users to submit jobs using DRMAA. Although the main server process runs as root, all it does is authenticate the user, then start another Java program which runs as that user and actually does the work in order to comply with the principle of minimising privileges. Initially, I was doing this with Runtime.exec() and sudo (example be...


java - How to give temporary privileges to Client using Spring and JSF

Hi i have a small application with spring and i've been adding security which is great but i need to allow temporary privileges to a specific client may generate an GUID to injecting into the URL, and send it to their email saying something like: Hi we need you to fill some information before start working on your task, please visit: http://mydomain.com/information/information.jsf?id=6bbdeb67986e405793fbf7...


java - loading dll which requires UAC elevated privileges with jni

I have a DLL which has certain functionality which requires UAC elevated privileges in order to work properly. I changed the manifest file to level=requireAdministrator, but when I load the dll in Java the functionality fails. Note that loading the dll works, but the implementation of the native methods execute as if there's no elevated privileges (doesn't even ask me to use UAC elevated privileges).


java - Android: how to tell if I have privileges to open a directory?

In my file browser, I can read the list of directories and files say in "/" but in that list, some directories require higher privileges to be opened and browsed (root, data etc). Is there a way to detect these before I display them? If the user can't open the directory, I don't want to display it. I think I'm looking for an AccessControlException but I'm not sure how and when I should be expecting it. Grat...


java - How to grant database privileges in DB2 to other Domain users

In a Java application, I have created 2 databases in DB2 with administrative authorities. Now using an application, want to access same databases over LAN but need to grant database privileges to other (in same Domain) Domain users, so that they can access the same databases as a end users. I have tried DB2 GRANT command, but it doesn't allow others to access those database, But if they use same user/pwd as of Admi...


netbeans - Java Project admin privileges windows 7

I am building a swing application (a file explorer) that has to copy/move files/folders around. When I try to copy to some folders such as Program Files, it throws an exception (access denied). I can solve by running NetBeans as administrator. Is there anyway I can give admin rights to my project only, without running the whole Virtual Machine as admin?






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top