5

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?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

4 Answers4

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Not true, since I could run this exe file with *ProcessBuilder*, but not in its own directory, as written above. – user1633807 Aug 29 '12 at 16:48
  • Thank you for editing your answer. As written above, I have one worry about new File() ? question : will this not delete my previous folder ? I have lot of datas stored in this, I do want to take the risk before running the code. Are you sure that it will not delete the previous folder "workingDir"? Thank you! – user1633807 Aug 29 '12 at 16:55
  • Even if you accidentally call `new File("C:\\TEST").delete();` it won't delete the directory unless its empty (you have to delete every file and directory in it first). Sounds like you need a backup. ;) If you are not sure, I would try it with a small copy of the files you need. ;) – Peter Lawrey Aug 29 '12 at 16:59
  • 1
    It works fine, thank you very much ! Since you both provided the same answer I didn't know to who assign the positive one. But I guess you less need points than fd. I hope you will understand. Thank you for your help anyway!!! – user1633807 Aug 29 '12 at 17:21
  • @user1633807 I would give rep to the lower points as well. :) – Peter Lawrey Aug 30 '12 at 05:33
1

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

Cratylus
  • 52,998
  • 69
  • 209
  • 339
1

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();
Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
  • new File() ? question : will this not delete my previous folder ? I have lot of datas stored in this, I do want to take the risk before running the code. Are you sure that it will not delete the previous folder "workingDir"? Thank you! – user1633807 Aug 29 '12 at 16:54
  • No, creating a new File object will not make any changes to the underlying files on disk, it just creates a representation of a file. To actually create a new file on disk there is an extra method call required (and even then, it should fail if the file already exists). – Mike Tunnicliffe Aug 29 '12 at 16:56
  • Ok, I've just tried and it works. Thank you very much. I don't know to who assign the correct answer since you both provided me the same and correct one. What should I do ? In any way, thank you for you help! – user1633807 Aug 29 '12 at 17:12
  • Which ever answer you think best describes the solution. If each answer only describes part of the solution, encourage the author to edit their answer to be more complete; or create your own answer that completely provides the solution and accept that. – Mike Tunnicliffe Aug 29 '12 at 17:16
1

Perhaps make a batch file with a cd and the command you want to run, then execute the batch file with cmd.

Sean W.
  • 4,944
  • 8
  • 40
  • 66