Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I elevate my UAC permissions from Java?

Tags:

java

windows

uac

I need to use the systemRoot feature of the Preferences API, but it fails due to lack of permissions on Windows if UAC is on. I'm trying to find the technical details of popping the UAC prompt and elevating my permissions to allow the systemRoot updates to succeed.

like image 579
Brian Knoblauch Avatar asked Jan 11 '11 20:01

Brian Knoblauch


3 Answers

According the accepted answer to this SO question, you cannot change the UAC permissions of a running process.

According to the answers to this SO question, possible ways to launch a process with elevated permissions are:

  • create a wrapper to launch the JVM (with the appropriate arguments!) with a windows manifest that requests raised privileges, or
  • use the application linked to the 2nd answer to run the JVM with raised privileges.
like image 126
Stephen C Avatar answered Nov 09 '22 23:11

Stephen C


In addition to the manifest Using JNI to call ShellExecute with verb = runas will also do this - but specifying things with a manifest is a more robust way of doing it. Getting a manifest embedded in an exe can be a bit tricky, and there were a lot of problems with manifest handling in earlier versions of Visual C++, but most of them are worked out now.

That said, I'd encourage you to think hard about why you need to access the system root - is it to store settings for all users? If so, you may want to consider having a separate application for managing those settings (with it's own manifest). You can't just pop open a UAC elevation dialog - you actually have to launch a new process (if you look at task manager with apps that appear to work this way, you'll see that a second instance of the app actually gets launched - look at the UAC Virtualization column in task manager to see the differences).

Another possibility is to adjust the security settings in the area of the registry that you absolutely must configure from your non-elevated process - but this goes against the design of UAC, and it'll almost always cause more trouble than it's worth. Probably better to drink the M$ kool-aid and design your app properly for UAC. (Believe me, I feel your pain on this - been through it a number of times).

As I was experiencing this pain myself, I found the following MSDN article quite helpful to understand the Microsoft design intent with UAC:

http://msdn.microsoft.com/en-us/library/aa511445.aspx

Hope this helps...

like image 41
Kevin Day Avatar answered Nov 09 '22 23:11

Kevin Day


You can use run-as-root library: https://github.com/dyorgio/run-as-root

// Specify JVM options (optional)
RootExecutor rootExecutor = new RootExecutor("-Xmx64m");

// Execute privileged action
rootExecutor.run(() -> System.out.println("Call your admin code here."));

P.S.: I'm the author.

like image 2
Dyorgio Avatar answered Nov 10 '22 01:11

Dyorgio