Java: run as administrator

Viewed 102728

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?

7 Answers

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!

Related