42

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?

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

7 Answers7

21

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

ZippyV
  • 12,540
  • 3
  • 37
  • 52
  • 8
    The problem of course is that the "executable" will be the JVM, not your own code. And since the manifest applies to the JVM, it applies to anything run by that JVM. – MSalters Sep 08 '09 at 10:59
  • 1
    What if you have a wrapper exe which launches java? Will the administrator access be inherited by the child process (jvm)? – Cal Oct 22 '09 at 03:01
  • 3
    @Cal - Yes - once a process is elevated, all processes it creates will be elevated. UAC elevation literally changes the user that the application is running as, and it is a one-way street - you can elevate, but you can not drop back down. – Kevin Day Jan 12 '11 at 04:32
10

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
LPL
  • 16,827
  • 6
  • 51
  • 95
Matt DeVillier
  • 101
  • 1
  • 2
8

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.

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • Thanks! You normally wouldn't want a user application to run as administrator (only certain privileged actions), so this is probably the more appropriate solution in most cases. I also like that the Elevate.exe has a -wait option so you can block until the elevated command has completed. – rob Jan 14 '11 at 20:33
  • if I read your link correctly, one must also modify the registry which may not always be an option. – likejudo Aug 13 '13 at 15:55
7

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");
mathd
  • 2,415
  • 2
  • 17
  • 8
3

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

Community
  • 1
  • 1
crig
  • 859
  • 6
  • 19
3

I will explain how do you get elevated java application

import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process <program.exe> -verb RunAs");
    }
}

program.exe is example just call notepad.exe

import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process notepad.exe -verb RunAs");
    }
}

Than you have got elevated program. I recommand you - You need convert to binary wrapper like Launch4j or Parcle Than you can click - If you create custom installer (made by Java Swing or Awt.)

I hope you have good solution :) - If you use Linux or Mac OS X than you need use Parcle binary wrapper or other wrapper if you know...

// EDIT good idea by Kaptkaos Than you write simple:

import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process -FilePath java.exe -Argument '-jar runasadmin.jar' -verb RunAs");
    }
}

Do not forget to use '' and "" if -jar <filename>.jar from -Argument of Start-Process. Note do not forget - If you use working directory and jar file must be in working directory example

  • .\myjar.jar
  • java .\RunAsAdminExample.class

Than you can see elevated java with argument :)

Best regards!

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
SourceSkyBoxer
  • 141
  • 1
  • 8
0

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.

Kaptkaos
  • 333
  • 3
  • 6
  • The author specifically mentiones "Of course without doing: Right click on the exe -> run as admin", so what's the point of this answer? – SeverityOne Oct 13 '20 at 07:46