Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Java application successfully close Windows Explorer, but fail to restart it again?

In my java application I use

Runtime.getRuntime().exec("taskkill /F /IM " + explorer.exe).waitFor();

to close Windows Explorer. This part works because Explorer realy stops working for me.

Unfortunately, when I use

Runtime.getRuntime().exec("explorer.exe");

Windows Explorer doesn't restart at all. How should I restart it properly?

like image 668
tb96 Avatar asked Dec 30 '12 17:12

tb96


Video Answer


1 Answers

I don't know the reason why that is not working for you, since you are executing an external program from java, I think it is not Java's fault that it is not restarting, maybe there is something wrong in the way you restart. I will give you an alternative:

Instead of using Runtime.exec(), try ProcessBuilder.start() for starting the process and use the method destroy() from the class Process to kill the process. It should work ok, just use the builder to create an object that represent explorer and assign it to a variable of type Process.

The only reason I think maybe it is easier if you use the ProcessBuilder is because it takes arguments as separate Strings, and maybe that makes it easier to configurate your scripts.

Example:

List<String> params = java.util.Arrays.asList("C:\myProgram.exe", "-arg1", "-arg2");
ProcessBuilder pb = new ProcessBuilder(params);
Process process = pb.start(); 

Give it a try I think it should work.

like image 180
javing Avatar answered Sep 25 '22 16:09

javing