Search code examples
javaexeexecutable-jarshutdownexe4j

shutdown.jar work but shutdwon.exe not work


I just created a GUI program for shutdown PC. It works fine, so I make a jar file of it which also works. Now I created an .exe file of it by using exe4j and lauch4j. It will start but when I click on shutdown button nothing happens, .exe program does not work. It irritates me, because no error message comes.

Here is a code.

public void actionPerformed(ActionEvent e){
String str=e.getActionCommand();
    if(e.getSource()==b1) {
        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("shutdown -s -t 0");
            System.exit(0);
        } catch(IOException e2) {
            e2.printStackTrace();
        }   
    }
}

Solution

  • Use the absolute path to shutdown.exe that you start from your Java application:

    runtime.exec(System.getenv("SystemRoot") + "\\System32\\"
                 + "shutdown -s -t 0");
    

    If your Java app is shutdown.exe, then runtime.exec really starts your Java application instead of the system utility.