gpt4 book ai didi

java - 有谁知道如何检测 Windows 服务是否通过 Java 运行

转载 作者:行者123 更新时间:2023-12-01 14:25:58 24 4
gpt4 key购买 nike

关于将 Java 应用程序作为服务运行的信息很多,但我需要知道如何检测 Windows 服务是否正在运行。有人知道吗???

在 DOS 提示符下,我可以运行:

tasklist /svc|findstr "NonRunningService"
echo Return code for N onRunningService is %ERRORLEVEL%
tasklist /svc|findstr "RunningService"
echo Return code for RunningService is %ERRORLEVEL%

我得到以下信息:

Return code for NonRunningService is 1
Return code for RunningService is 0

在代码中,我有:

int retCode = Runtime.getRuntime.exec("tasklist /svc|findstr \"NonRunningService\"").waitFor();
System.out.println("Return code for NonRunningService is " + retCode);
retCode = Runtime.getRuntime.exec("tasklist /svc|findstr \"RunningService\"").waitFor();
System.out.println("Return code for RunningService is " + retCode);

我得到以下输出

Return code for NonRunningService is 1
Return code for RunningService is 1

根据 JavaDocs,waitFor() 应该阻塞直到进程完成,并给我进程的退出值。

我也尝试过使用 Process/ProcessBuilder 命令行调用:

//'tasklist /nh /fi "SERVICES eq RunningService"' will return a line for 
// each running service of the requested type.
Process p1 = new ProcessBuilder("tasklist", "/nh", "/fi" "SERVICES eq RunningService").start();
p1.waitFor();
BufferedReader is = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line = is.readLine();
System.out.println("Service - " + line);
System.out.println("Running? ", (line==null?"No":"Yes");

给出:

Service -
Running? No

即使我在命令行的输出中得到行!

最佳答案

我重新创建了您的代码并通过创建一个类来打印进程的输出来添加一些额外的调试输出:

private static class Writer implements Runnable {

private InputStream is;

public Writer(InputStream is) {
this.is = is;
}

@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

在 main 方法中,我在调用 waitFor() 之前启动了一个实例:

public static void main(String[] args) throws Exception {
String command = "tasklist /svc | findstr \"svchost.exe\"";
Process p = Runtime.getRuntime().exec(command);

new Thread(new Writer(p.getInputStream())).start();
new Thread(new Writer(p.getErrorStream())).start();

System.out.println("Return code: " + p.waitFor());

}

输出结果是:

ERROR: Invalid argument/option - '|'.
Type "TASKLIST /?" for usage.

因为命令不是在shell中执行的,所以首先需要调用Windows shell,并将命令作为参数传入:

String command = "cmd /c tasklist /svc | findstr \"svchost.exe\"";

更改此设置后,输出现在为 返回代码:0

然而,我发现了一个奇怪的问题,如果您不处理进程的标准输出 channel 的输出,由于某种原因进程不会终止。为了解决这个问题,我不得不放入一个循环来读取和丢弃该过程的输出:

public static void main(String[] args) throws Exception {
String command = "cmd /c tasklist /svc | findstr \"svchost.exe\"";
Process p = Runtime.getRuntime().exec(command);
while(p.getInputStream().read() != -1) {} //hangs without this line

System.out.println("Return code: " + p.waitFor());

}

关于java - 有谁知道如何检测 Windows 服务是否通过 Java 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2629922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com