Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I think the problem lies in that after you killed [explorer.exe] you cannot retrieve a new runtime environment. What worked for me was to reuse the runtime environment I used to kill [explorer.exe]. I found this useful for being able to edit certain system resources from within Java(TM).

public class KillRestart {
   public static void main (String [] args) {
      Runtime runtime = Runtime.getRuntime ();

      try {
         System.out.println ("Killing Explorer.exe process ... ");
         runtime.exec ("taskkill /F /IM explorer.exe /T").waitFor ();

         // Do something wild like update system resources

         System.out.println ("Resurrecting Explorer ...");
         runtime.exec ("explorer.exe");
      }
      catch (InterruptedException e) {
         e.printStackTrace ();
      }
      catch (IOException e) {
         e.printStackTrace ();
      }
   }
}

Finally,I find a method that work for me, powershell command "Stop-Process" help me and the command must have "-force" parameter. check Stop-Process command

   //java program
   ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "powershell","Stop-Process - 
   ProcessName explorer -Force");
   Process p = pb.start();
 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top