title: How to close all the open application on windows using java?
body: <p>I have made a package of exe installer of the following code to close all the open application on windows platform.
  The exe is working fine on the machine on which its is written i.e. Windows 8.1 Pro (32 bit OS / X64 based processor)
  But the same exe is not working on Windows 7 or different machine. </p>
  
  <pre><code>try {
              String line;
              Process p = Runtime.getRuntime().exec("tasklist.exe");
              Runtime rt = Runtime.getRuntime();
              BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
              while ((line = input.readLine()) != null) {
  
                  if (line.toLowerCase().contains(".exe".toLowerCase()) &amp;&amp; line.toLowerCase().contains("Console".toLowerCase())) {
                      System.out.println(line);
                      String pocessKill = line.substring(0, line.indexOf(".exe"));
                      pocessKill = pocessKill + ".exe";
  
                      if (pocessKill.equalsIgnoreCase("netbeans.exe") || pocessKill.equalsIgnoreCase("java.exe") || pocessKill.equalsIgnoreCase("csrss.exe") || pocessKill.equalsIgnoreCase("winlogon.exe") || pocessKill.equalsIgnoreCase("dwm.exe") || pocessKill.equalsIgnoreCase("taskhostex.exe") || pocessKill.equalsIgnoreCase("explorer.exe") || pocessKill.equalsIgnoreCase("igfxtray.exe") || pocessKill.equalsIgnoreCase("hkcmd.exe") || pocessKill.equalsIgnoreCase("igfxpers.exe") || pocessKill.equalsIgnoreCase("jusched.exe") || pocessKill.equalsIgnoreCase("GWX.exe") || pocessKill.equalsIgnoreCase("rundll32.exe") || pocessKill.equalsIgnoreCase("conhost.exe") || pocessKill.equalsIgnoreCase("tasklist.exe") || pocessKill.equalsIgnoreCase("conhost.exe")) {
                          System.out.println(pocessKill);
                      } else {
                          rt.exec("taskkill /F /IM " + pocessKill);
                      }
                  } else {
                      System.out.println("Bad Process");
                  }
  
              }
              input.close();
  
              System.out.println(rt.toString());
  
          } catch (Exception err) {
              err.printStackTrace();
          }
  </code></pre>
  
  <p>On windows 8 its working fine, but on windows 7 its showing exceptions as follows:
  (Output + Exception)</p>
  
  <pre><code>Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  csrss.exe                      936 Console                    1     22,612 K
  csrss.exe
  Bad Process
  winlogon.exe                  1016 Console                    1      3,508 K
  winlogon.exe
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  dwm.exe                       1928 Console                    1     33,116 K
  dwm.exe
  explorer.exe                  1944 Console                    1     60,328 K
  explorer.exe
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  Bad Process
  PWRISOVM.EXE                  2152 Console                    1      3,456 K
  java.lang.StringIndexOutOfBoundsException: String index out of range: -1
      at java.lang.String.substring(String.java:1955)
      at decryptomediaplayer.DecryptoMediaPlayer.main(DecryptoMediaPlayer.java:731)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
      at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
      at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = main
      at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:204)
      at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:438)
      at javafx.stage.Stage.&lt;init&gt;(Stage.java:238)
      at javafx.stage.Stage.&lt;init&gt;(Stage.java:224)
      at javafx.scene.control.HeavyweightDialog$1.&lt;init&gt;(HeavyweightDialog.java:50)
      at javafx.scene.control.HeavyweightDialog.&lt;init&gt;(HeavyweightDialog.java:50)
      at javafx.scene.control.Dialog.&lt;init&gt;(Dialog.java:262)
      at javafx.scene.control.Alert.&lt;init&gt;(Alert.java:245)
      at javafx.scene.control.Alert.&lt;init&gt;(Alert.java:223)
      at decryptomediaplayer.DecryptoMediaPlayer.main(DecryptoMediaPlayer.java:750)
      ... 11 more
  Exception running application decryptomediaplayer.DecryptoMediaPlayer
  Java Result: 1
  </code></pre>