在Windows 10上,可以使用以下命令检测服务是否已启动:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ServiceCheck {

    public static void main(String[] args) {
        String serviceName = "YourServiceName"; // 替换为你要检测的服务名称

        try {
            Process process = Runtime.getRuntime().exec("sc query " + serviceName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                if (line.trim().startsWith("STATE")) {
                    String[] state = line.split(":");
                    String serviceState = state[1].trim();

                    if (serviceState.equalsIgnoreCase("RUNNING")) {
                        System.out.println("Service is running.");
                    } else {
                        System.out.println("Service is not running.");
                    }

                    break;
                }
            }

            reader.close();
            process.destroy();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请将YourServiceName替换为你要检测的服务名称。然后运行这个Java程序,它将输出服务是否正在运行

jdk8+Win10检测服务是否已启动的命令代码

原文地址: https://www.cveoy.top/t/topic/hUUa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录