1

So, I have started a process in java using exec method in Runtime class. Now, how I invoked this is as follows.

Runtime r=Runtime.getRuntime(); 
Process p=null; 
String s="E:\\Softwares\\fiddler4setup.exe"; 
p=r.exec("cmd /c"+s);

Now, when I trying to destroy the process using p.destroy() method. Fiddler4setup.exe is still running. I have been searching ways to close this for like a week. Even here, there is no particular correct answer for this sort of problem. Please kindly help me with this.

Also, running the file directly is not working as it's displaying some 704 error I guess. Then, I saw this solution of using "cmd /c".

Thank you.

  • 1
    It should be possible to run a binary Exe directly without using cmd.exe... So I would first debug why that call did not work. – GhostCat Mar 10 '17 at 18:23
  • I have debugged it. As far as I know, this process cannot be terminated and the messaged stated was "Access is denied" when I tried this in cmd @GhostCat –  Mar 10 '17 at 18:36
  • @GhostCat , it stats an error that: CreateProcess error=740, The requested operation requires elevation –  Mar 10 '17 at 18:38
  • There we go. So simply start your Java program with admin rights ; respectively ask your users to do so. Obviously elevation is nothing that any program could trigger itself. – GhostCat Mar 10 '17 at 19:57

1 Answers1

1

To kill your process you have to use :

Taskkill /IM YourProcess.exe /F

Or with PID

Taskkill /PID 26356 /F

So your program should look like this :

Runtime r = Runtime.getRuntime();
Process p = r.exec("Taskkill /IM fiddler4setup.exe /F");

Note you can get the PID of your process using this command tasklist in your cmd, this will be better if you use PID to avoid all problem of path.


EDIT

There no clear way to run your script as administrator mode, there are some question here hope can gives you and idea :

Run command prompt as Administrator
Java: run as administrator
Run Java file as Administrator with full privileges

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • try without path, like i do, or search the PID of your task and kill it like i do @SunilSonu – Youcef LAIDANI Mar 10 '17 at 18:42
  • I am getting the PID of fiddler4setup using tasklist and then using the above code, yet it's not working. @YCF_L –  Mar 10 '17 at 18:45
  • Does it work when you run that command on a shell directly? Instead of calling it from Java? – GhostCat Mar 10 '17 at 18:50
  • i try this with java and with cmd but with another process not with `fiddler4setup` and it work fine, i think it is the same logic @GhostCat or i'm wrong? – Youcef LAIDANI Mar 10 '17 at 18:52
  • Even in shell, it says "Access is denied" @GhostCat –  Mar 10 '17 at 18:53
  • i try with name and with PID and it work perfectly both in cmd and java – Youcef LAIDANI Mar 10 '17 at 18:53
  • aaahh then in this case you have to execute it in administrator mode @SunilSonu – Youcef LAIDANI Mar 10 '17 at 18:54
  • @YCF_L Can you share the code, so that I can test it once. –  Mar 10 '17 at 18:54
  • @YCF_L It works well in administrator mode, but how to run taskkill command in administrator mode using java. –  Mar 10 '17 at 18:55
  • this is the problem now @SunilSonu let me make some research and when i find i will tell you, actually i don't have any idea how to run it in administrator mode – Youcef LAIDANI Mar 10 '17 at 18:57
  • I guess you have to make sure that your Java program is initially invoked with admin privileges. You probably want to check right on startup if your Java code is running with admin rights. – GhostCat Mar 10 '17 at 19:55
  • yes @GhostCat this is a solution, i find also other solution, like create a script witch enable the admin mode but no clear way – Youcef LAIDANI Mar 10 '17 at 19:57
  • @GhostCat Can you please give an example for that? –  Mar 11 '17 at 06:16
  • I am not aware of doing that programmatically. You would have to use "run as admin" when clicking your icon. Maybe you need a new question if you want to somehow make the Java program do that by itself. – GhostCat Mar 11 '17 at 06:20
  • @SunilSonu there are no clear way to run your script as administrator mode you can check my edit hope this links gives you an idea – Youcef LAIDANI Mar 11 '17 at 07:43