I tried to run this Java app:
public class Run_Process {
public static void main(String[] args) {
String myCommandIp = "netsh interface ip set address name=\"Ethernet\" static 192.168.0.4 255.255.255.0 192.168.0.1 1";
String myCommand2Dns = "netsh interface ipv4 add dnsserver \"Ethernet\" address=192.168.0.1 index=1";
runCommand(myCommandIp);
runCommand(myCommand2Dns);
}
static void runCommand(String command) {
try {
new ProcessBuilder(command.split(" ")).redirectError(ProcessBuilder.Redirect.INHERIT)
.redirectOutput(ProcessBuilder.Redirect.INHERIT).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
but I get:
The requested operation requires elevation(Run as administrator)
How to launch my app again, requesting elevation with the 'run as' verb? This is how I did it in Python. However, I need help to do it in Java.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import ctypes, sys, subprocess
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
subprocess.call(['wmic', '/output:usracc.txt', 'useraccount', 'list', 'full', '/format:csv'])
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
This has been asked so many times, but I need a concrete example for my case because I'm noob.
I will explain why this is not a duplicate of linked question. Using a shortcut is not an option. I have to assume that the user doesn't know how to create a shortcut. JNA
and wizmo
solutions are not explained. Here it says that:
So, in the Java world, you just have to do exactly what everyone else has to do: launch your app again, requesting elevation. There are several ways to launch elevated, the 'run as' verb probably being the easiest.
So, I ask for help how to use a solution with 'run as' verb and ShellExecute in Java.
Ok, this is an answer to my question. I will leave this here for future reference. Firstly, I downloaded JNA from here. Run them with java -jar jna.jar
and imported in my Eclipse project.
This will open cmd as admin if you answer yes
in UAC and then it will run netsh
command. It's the runas
which starts UAC. You can use open
to start cmd as normal user.
This SO answer helped me a lot with jna
, a this one too with passing arguments in ShellExecute. Here you can read what /S
and /C
do.
import com.sun.jna.*;
import com.sun.jna.platform.win32.ShellAPI;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.*;
public class Main {
public interface Shell32 extends ShellAPI, StdCallLibrary {
Shell32 INSTANCE = (Shell32)Native.loadLibrary("shell32", Shell32.class);
WinDef.HINSTANCE ShellExecuteA(WinDef.HWND hwnd,
String lpOperation,
String lpFile,
String lpParameters,
String lpDirectory,
int nShowCmd);
}
public static void main(String[] args) {
WinDef.HWND h = null;
Shell32.INSTANCE.ShellExecuteA(h, "runas", "cmd.exe", "/S /C \"netsh interface ip set address name=\"Wi-Fi\" static 192.168.88.189 255.255.255.0 192.168.88.1 1\"", null, 1);