Search code examples
javawindowsadmin

Elevate Java application while running


A nasty problem popped out with my software. I am making a program that interacts with another existing software (a game). User has reported that he runs the game with administrator privileges and under that circumstances, my program stops working for him.

Short investigation revealed that some people really need to run the game under administrator account and some don't. It would be great if my program would be able to detect this and warn user if the game is running under administrator account:

image description

If the user clicks "Elevate", I'd like to ask windows to elevate the java.exe running my jar file and invoke the typical UAC dialog.

image description
Obviously, this time the question would not be about java updater but JRE

My question is: Is this possible? Can windows elevate my java.exe instance's privilege? Does java have a way to do it? Or can I use command line command?

I want to avoid restarting the program (though it wouldn't probably be such a big deal).

Edit: If you look in the comments, you'll see that there's no avoiding the restart of an application - process can only start elevated, not become elevated. This kinda shifts the question, unfortunately. Basically, it now sounds more like: "How to restart my application with admin rights?". Unless, of course, there's a trick like two java.exe sharing one jar...


Solution

  • As has been pointed in comments, sadly the Java (or any other process) cannot be elevated while running. While in the case of JWM, it could be theoretically possible to move whole program context from normal user java.exe to elevated one, I don't think it's possible. I hope some day someone will come and tell me I'm wrong.

    Surprisingly, even with restart in place, this was a tricky task that took me a while to figure out.

    The non java part

    First, how do we exactly run a program elevated from command line? There's an answer and you can see it's not simple. But we can break it to this VBS script:

    Set UAC = CreateObject("Shell.Application") 
    UAC.ShellExecute "program name", "command line parameters", "working directory", "runas", 1 
    

    Soon, it also turns out that we won't have any success running java.exe from VBS script. In the end, I decided to run a helper batch file. Finally, here (answer to question in the last link) we have a complete set of two scripts which really run the given .jar file elevated. Here's improved version that allows quick testing by drag'n'dropping the Jar file on it:

    ' Require first command line parameter
    if WScript.Arguments.Count = 0 then
      MsgBox("Jar file name required.")
      WScript.Quit 1
    end if
    
    ' Get the script location, the directorry where it's running
    Set objShell = CreateObject("Wscript.Shell")
    
    strPath = Wscript.ScriptFullName
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objFile = objFSO.GetFile(strPath)
    strFolder = objFSO.GetParentFolderName(objFile) 
    'MsgBox(strFolder)
    
    ' Create the object that serves as runnable something
    Set UAC = CreateObject("Shell.Application") 
    ' Args:
    '   path to executable to run
    '   command line parameters - first parameter of this file, which is the jar file name
    '   working directory (this doesn't work but I use it nevertheless)
    '   runas command which invokes elevation
    '   0 means do not show the window. Normally, you show the window, but not this console window
    '     which just blinks and disappears anyway
    UAC.ShellExecute "run-normally.bat", WScript.Arguments(0), strFolder, "runas", 0 
    
    WScript.Quit 0
    

    The Java part

    Java part is more straightforward. What we need to do is to open new process and execute the prepared scripts in it.

       /**
        * Start this very jar file elevated on Windows. It is strongly recommended to close any existing IO
        * before calling this method and avoid writing anything more to files. The new instance of this same
        * program will be started and simultaneous write/write or read/write would cause errors.
        * @throws FileNotFoundException if the helper vbs script was not found
        * @throws IOException if there was another failure inboking VBS script
        */
       public void StartWithAdminRights() throws FileNotFoundException, IOException {
         //The path to the helper script. This scripts takes 1 argument which is a Jar file full path
         File runAsAdmin = new File("run-as-admin.vbs");;
         //Our 
         String jarPath;
    
         //System.out.println("Current relative path is: " + s);
    
         try {
           jarPath = "\""+new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath()+"\"";
         } catch (URISyntaxException ex) {
           throw new FileNotFoundException("Could not fetch the path to the current jar file. Got this URISyntax exception:"+ex);
         }
         //If the jar path was created but doesn't contain .jar, we're (most likely) not running from jar
         //typically this happens when running the program from IDE
         //These 4 lines just serve as a fallback in testing, should be deleted in production
         //code and replaced with another FileNotFoundException
         if(!jarPath.contains(".jar")) {
           Path currentRelativePath = Paths.get("");
           jarPath = "\""+currentRelativePath.toAbsolutePath().toString()+"\\AutoClient.jar\"";
         }
         //Now we check if the path to vbs script exists, if it does we execute it
         if(runAsAdmin.exists()) {
           String command = "cscript \""+runAsAdmin.getAbsolutePath()+"\" "+jarPath;
           System.out.println("Executing '"+command+"'");
           //Note that .exec is asynchronous
           //After it starts, you must terminate your program ASAP, or you'll have 2 instances running
           Runtime.getRuntime().exec(command);
    
         }
         else
           throw new FileNotFoundException("The VBSScript used for elevation not found at "+runAsAdmin.getAbsolutePath());
       }