UAC and Java

那年仲夏 提交于 2019-11-28 10:52:45

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.

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?

You have to use an external (native) application to do this. This post provides source code and a great explanation: UAC Prompt From Java

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.

Goto the folder where java is installed. open the properties of javaw.exe / java.exe and set "run as administator" option true.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!