简体   繁体   English

如何使用Java关闭Windows上所有打开的应用程序?

[英]How to close all the open application on windows using java?

I have made a package of exe installer of the following code to close all the open application on windows platform. 我制作了以下代码的exe安装程序包,以关闭Windows平台上的所有打开的应用程序。 The exe is working fine on the machine on which its is written ie Windows 8.1 Pro (32 bit OS / X64 based processor) But the same exe is not working on Windows 7 or different machine. 该exe在运行其的计算机上运行良好,即Windows 8.1 Pro(基于32位OS / X64的处理器),但同一个exe在Windows 7或其他计算机上无法运行。

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()) && 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();
        }

On windows 8 its working fine, but on windows 7 its showing exceptions as follows: (Output + Exception) 在Windows 8上可以正常工作,但在Windows 7上则显示如下异常:(输出+异常)

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.<init>(Stage.java:238)
    at javafx.stage.Stage.<init>(Stage.java:224)
    at javafx.scene.control.HeavyweightDialog$1.<init>(HeavyweightDialog.java:50)
    at javafx.scene.control.HeavyweightDialog.<init>(HeavyweightDialog.java:50)
    at javafx.scene.control.Dialog.<init>(Dialog.java:262)
    at javafx.scene.control.Alert.<init>(Alert.java:245)
    at javafx.scene.control.Alert.<init>(Alert.java:223)
    at decryptomediaplayer.DecryptoMediaPlayer.main(DecryptoMediaPlayer.java:750)
    ... 11 more
Exception running application decryptomediaplayer.DecryptoMediaPlayer
Java Result: 1

The reason is simple. 原因很简单。 Your code cannot handle processes in uppercase letters. 您的代码无法处理大写字母的进程。

String line = "PWRISOVM.EXE   2152 Console";
String pocessKill = line.substring(0, line.indexOf(".exe"));

this will lead into 这将导致

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

you are getting the exception at this line: 您在此行遇到异常:

  String pocessKill = line.substring(0, line.indexOf(".exe"));

The problem I see here is that the name PWRISOVM.EXE could not be converted to lower case and thus it could not find ".exe" as a result it gave an exception for 我在这里看到的问题是名称PWRISOVM.EXE无法转换为小写,因此找不到“ .exe”,因此它为

    line.substring(0, line.indexOf(".exe"));

There are multiple possibilities for this error. 此错误有多种可能性。 I would suggest you to give some more debugging information so that you can narrow the question. 我建议您提供更多调试信息,以便缩小问题范围。 Firstly, you can check the java version in which you created your exe and the java version of the machine you are trying to run it on. 首先,您可以检查创建exe的Java版本以及要在其上运行它的计算机的Java版本。 Secondly, try debugging your code step by step using the debug options in any standard IDE like Eclipse. 其次,尝试使用任何标准IDE(如Eclipse)中的调试选项逐步调试代码。

You can edit your question after trying these things and provide more info about your problem. 您可以在尝试这些方法后编辑问题,并提供有关问题的更多信息。 Also post what is your input. 还发布您的输入内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM