問題描述
在具有提升權限的自己的目錄中啟動 .exe (Launching .exe in its own directory with elevation priviledges)
My question is pretty simple, I would like to launch a .exe in its own directory but with elevation rights/privileges. I know that this question as been raised before but I didn't found the right way for fixing my problem.
Indeed, I first tried this :
String workingDir = "C:\\TEST\\";
String cmd = workingDir + "game.exe";
Runtime.getRuntime().exec(cmd,null,new File(workingDir));
I got the following error:
CreateProcess error=740, The requested operation requires elevation
Then I tried this:
ProcessBuilder builder = new ProcessBuilder(
new String[] {"cmd.exe", "/C","C:\\TEST\\game.exe"});
Process newProcess = builder.start();
And it runs but not in its own directory. How can I fix this please?
‑‑‑‑‑
參考解法
方法 1:
It appears you want to set
builder.directory(new File("C:\\TEST"));
which
Sets this process builder's working directory
Otherwise, it appears that for this to work you need to be Running as an Administrator.
https://www.google.co.uk/search?q=CreateProcess+error%3D740%2C+The+requested+operation+requires+elevation
方法 2:
I don't think it is possible to elevate privileges of a forked process. You should start the new process with the account that has the rights you need
方法 3:
I wonder if this will work:
String workingDir = "C:\\TEST\\";
ProcessBuilder builder = new ProcessBuilder(
new String[] {"cmd.exe", "/C",workingDir+"game.exe"}
);
builder.directory(new File(workingDir));
Process newProcess = builder.start();
方法 4:
Perhaps make a batch file with a cd and the command you want to run, then execute the batch file with cmd.
(by user1633807、Peter Lawrey、Cratylus、Mike Tunnicliffe、Sean W.)