Is it possible to ask for elevated permissions from within a Java Application? Suggestions I've seen seem to all be centered around running an external executable or setting up a manifest to request privileges on launch. These aren't available to, for instance, applets. Is there any way to request elevation from a running application?
-
See http://stackoverflow.com/questions/1385866/java-run-as-administrator/1385901#1385901, which suggests using an Elevate.exe utility to launch a separate elevated process to perform the privileged actions. – rob Jan 14 '11 at 20:39
-
This can be done via JNA. See [this](http://stackoverflow.com/questions/11041509/elevating-a-processbuilder-process-via-uac) – dARKpRINCE May 09 '14 at 12:07
5 Answers
UAC is not something that a running process can request (doesn't matter what language you are running in). You have to request elevation at launch time.
The way that most windows apps handle this (and make it look like they are requesting elevation) is to spawn an extra copy of themselves, requesting elevation, and passing sufficient command line arguments to the new process so that the appropriate dialog can be displayed.
This is why UAC elevation in a dialog is always initiated by a button click that opens a new dialog.
So, in the Java world, you just have to do exactly what everyone else has to do: launch your app again, requesting elevation. There are several ways to launch elevated, the 'run as' verb probably being the easiest.

- 16,067
- 8
- 44
- 68
-
1More details on the elevation request please. I'm coming up empty on Google and trying to use "runas" just fails (it's looking for a password from the CLI instead of popping the dialog). – Brian Knoblauch Jan 11 '11 at 16:18
-
@Brian - does this help? http://stackoverflow.com/questions/1797695/how-to-runlaunch-elevated-command-promt-programmiticaly-in-vista - if not, create a new question with the code you are using and post the link to this comment thread and I'll see what help I can offer. – Kevin Day Jan 12 '11 at 00:17
-
@Kevin Afraid not. I'm not using .Net, I'm using Java. Also, I'm already doing the Java equivalent of that. I'm able to launch whatever I want at the same elevation level, but when I try to escalate it that way from my app, it just bounces back immediately without an error and without doing anything. From the command line it throws a command line password prompt. – Brian Knoblauch Jan 12 '11 at 13:16
-
@brian - Yah - that's kinda my point - you are trying to do a highly platform specific operation from Java - that's going to require native code, pretty much any way you cut it. Either write a small JNI implementation, or consider using JNA (https://jna.dev.java.net/) - for this sort of thing, JNA would be a very good fit. Call ShellExecute with a runas verb. – Kevin Day Jan 16 '11 at 04:01
-
@brian - also, I'm assuming you are launching javaw.exe (and not java.exe), right? – Kevin Day Jan 16 '11 at 04:02
-
I was using java.exe. Have now abandoned trying to use the Java Preferences library and have gone to configuration files, with platform specific determination of location. That works fine with no escalation issues. – Brian Knoblauch Jan 17 '11 at 14:16
-
java.exe cannot display a UAC prompt - you should have been using javaw.exe – Kevin Day Jan 19 '11 at 06:07
-
@KevinDay `JNA would be a very good fit. Call ShellExecute with a runas verb.` Can you help me how to do this? – Hrvoje T May 11 '18 at 18:42
-
@Hrovje I'm happy to offer suggestions, but probably best to ask a new question (you can link back to it here) and include the code you've tried already. – Kevin Day May 12 '18 at 00:47
Looks like Sun will have to handle that kind of situation in the JRE since there's no other way of doing elevated actions than by running an external process.
If JRE supported it, JVM would probably have to run a separate, elevated process for the java code requesting the elevation.
For now however, only the manifest or running an external application are the only solutions available as far as I know.
The question is, what do you need elevation for?

- 16,086
- 6
- 47
- 54
-
2In the past, we were able to use signed applets to install and load some native code. This is no longer possible on Vista with UAC enabled even though Java's security model ostensibly allows it. – joegester Jul 02 '09 at 21:56
You have to use an external (native) application to do this. This post provides source code and a great explanation: UAC Prompt From Java

- 106
- 2
- 11
You could use jna and do a ShellExec. For lpOperation use "runas" (this is not documented). Since you likely need the full path to the (current) JavaVM which is stored in the registrylook at registry access, part of JNA.

- 15,196
- 1
- 37
- 55
Goto the folder where java is installed. open the properties of javaw.exe / java.exe and set "run as administator" option true.

- 23
- 1
- 6