Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: run as administrator

Is there a way in Java to ask the system to get control over administrator functionality. Of course without doing: Right click on the exe -> run as admin.
What I want is that there comes a frame from UAC like in Windows Vista or Windows 7.

Or have I to do some settings while making an exe from the jar?

like image 826
Martijn Courteaux Avatar asked Sep 06 '09 15:09

Martijn Courteaux


People also ask

How do I run a .jar file as administrator?

First: open CMD as administrator and open jar:Start > cmd > right click > run as administrator.

How do I run a command prompt as administrator in Java?

Runtime. getRuntime(). exec("c:/elevate Rundll32.exe Powrprof. dll,SetSuspendState ");

How do I force an app to run as administrator?

Again, right-click on the app's name. Click on Properties and select the Shortcut tab. Select Advanced. Finally, mark the checkbox next to Run as administrator.


2 Answers

You have to create a manifest file that specifies that your application needs administrator permissions. You can include the manifest in your exe or keep it as a separate file (yourapp.exe.manifest)

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

like image 198
ZippyV Avatar answered Sep 21 '22 17:09

ZippyV


The easiest way would be to use a wrapper to launch your JVM, then elevate the wrapper. I use a simple NSIS installer script with the UAC plugin to do this:

; Java Launcher ;--------------  ; You want to change the below lines    Name "my program"    Caption "my program Launcher"     Icon "iconfile.ico"     OutFile "java launcher.exe"  ; param below can be user, admin     RequestExecutionLevel admin  !include UAC.nsh  SilentInstall silent AutoCloseWindow true ShowInstDetails show  Section ""       ; command to execute       StrCpy $0 'javaw -jar myjarfile'         SetOutPath $EXEDIR       Exec $0     SectionEnd 
like image 39
Matt DeVillier Avatar answered Sep 21 '22 17:09

Matt DeVillier