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?


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


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

ZippyV's answer is fine if you intend to launch the javaw.exe with system admin privileges, which if pure java code is what is getting blocked by the UAC (such as trying to write a file to a privileged directory), then that is what you will need to do.

If, however, you are trying to launch something external, but just with elevation, you could bundle an exe which elevates a command. Here is one.


You can use a windows program to elevate your privilege. The program will show the UAC prompt and then you will have admin privilege.

http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/

You can then run for command like this:

Runtime.getRuntime().exec("Elevate.exe yourcommand");

If you can run whatever you need to run from a windows CMD batch file.. You can create a batch file (on the fly even) from within your java app that does what you want and launch it. I have it working so my java program launches this and it pops up the normal windows UAC prompt to the user and then runs the commands if they choose YES.

    String[] commands = {"cmd.exe", "/C" "mybatchfile.bat"};
    ProcessBuilder pb = new ProcessBuilder(commands);
    ..
    pb.start();

Here is a thread that deals with requesting UAC access within a windows batch file: How to request Administrator access inside a batch file

NOTE: ProcessBuilder can be a little tricky to deal with, using a StreamGobbler to handle the output works for me: Handle Input using StreamGobbler