1

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.

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41
  • 1
    Possible duplicate of [Run command prompt as Administrator](https://stackoverflow.com/questions/14596599/run-command-prompt-as-administrator) – explv May 11 '18 at 12:08
  • If you know that it was already asked and you looked at the existing questions, then please tell us where exactly you have problems. We are not supposed to code for you. The duplicate does tell what you need to know. Try it and if you still have problems come with them specifically. – findusl May 11 '18 at 12:11
  • No, the duplicate doesn't tell the answer. I can't create a shortcut or right click with 'run as admin'. It has to be double-click, and click "Yes" to UAC. There is no an answer for that. – Hrvoje T May 11 '18 at 12:15
  • @HrvojeT there are possible solutions past the accepted answer in the duplicate thread. If that doesn't help you, perhaps you should look into using something like launch4j: https://stackoverflow.com/questions/19037339/run-java-file-as-administrator-with-full-privileges/22110928#22110928 – explv May 11 '18 at 14:13
  • No there are not. I need a solution with 'run as' in ShellExecute. – Hrvoje T May 11 '18 at 18:11

1 Answers1

2

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);
Hrvoje T
  • 3,365
  • 4
  • 28
  • 41