java run as administrator

java run as administrator  using -'java,windows,admin,uac,exe'

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?

asked Sep 16, 2015 by deepak
0 votes

8 Answers

0 votes

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)

answered Sep 16, 2015 by balvant maurya
0 votes

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.

answered Sep 16, 2015 by thiru
0 votes

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    
answered Sep 16, 2015 by 20shahi
0 votes

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

You can then run for command like this:

Runtime.getRuntime().exec("Elevate.exe yourcommand");
answered Sep 16, 2015 by badhwar.rohit
0 votes

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);

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

answered Sep 16, 2015 by suyesh.lokhande
0 votes

I use the command line on windows for this purpose. From the Start menu, type cmd in the textbox and when the program cmd.exe appears, right click on it and select Run as Administrator. Now when ytou execute java -jar itwill run with Admin rights.

answered Sep 16, 2015 by yashwantpinge
0 votes

Access java.exe and javaw.exe inside the installation folder. i.e - C:\Program Files\Java\jre7\bin or C:\Program Files\Java\jdk1.7.0_17\bin left click on them and select properties->compatability and there: check "run this program as administrator".

answered Sep 16, 2015 by vimaldas2005
0 votes

If you really know what you are doing, then start a console in windows as administrator, i.e.: Click Start => Programs => Accessoires (right click on "Command Prompt") and then click on "Run as Administrator"

This will open a Command Prompt (cmd.exe) with admin rights. Change the directory to the location where the JAR file is located which you want to execute. For example if the JAR is in "D:\foo\bar" then you type: D: [ENTER] cd \foo\bar [ENTER]

From there you may run the JAR executable file "executable.jar" by using: java -jar executable.jar (obviously this assumes that your PATH variable contains the path to your JRE/JDK binaries; I will not go into this at this point)

answered Sep 16, 2015 by santosh soni