Elevating a ProcessBuilder process via UAC?

前端 未结 2 1804
春和景丽
春和景丽 2020-11-30 06:18

I\'m trying to run an external executable, but apparently it needs elevation. The code is this, modified from an example of using ProcessBuilder (hence the array with one a

2条回答
  •  被撕碎了的回忆
    2020-11-30 06:43

    Prunge's answer works fine for me.But after doing a bit more research ,I found out another approach using vb script and batch file.I prefer this approach because using vb-script does not cause black cmd window to pop up every time,when I open my application.

    1. Create an ordinary bat file to open the external executable file. I am going to open a MySQL server executable file

      @echo off
      cd "C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin"
      :: Title not needed:
      start /MIN  mysqld.exe
      exit
      
    2. Save it as mysql.bat

    3. Now create a vb script with admin privileges and at the end add script to open mysql.bat file.

    At the end CreateObject("Wscript.Shell").Run runs the bat file mysql.bat.

    Set WshShell = WScript.CreateObject("WScript.Shell")'
     If WScript.Arguments.length = 0 Then
        Set ObjShell = CreateObject("Shell.Application")
        ObjShell.ShellExecute "wscript.exe", """" & _
        WScript.ScriptFullName & """" &_
        " RunAsAdministrator", , "runas", 1
        Wscript.Quit
        End if
        CreateObject("Wscript.Shell").Run "C:\Users\Shersha\Documents\NetBeansProjects\Berries\batch\mysql.bat",0,True
    
    1. Now save this file as mysql_start.vbs
    2. Finally Run vb script from java .

    Thats it

     try {
        Runtime.getRuntime().exec("wscript C:\\\\Users\\\\Shersha\\\\Documents\\\\NetBeansProjects\\\\Berries\\\\batch\\\\mysql_start.vbs");
            } catch (IOException e) {
                                System.out.println(e);
                                System.exit(0);
            }
    

提交回复
热议问题