在Windows 10上,可以使用以下Java代码检测服务是否已启动:\n\njava\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\n\npublic class ServiceCheck {\n\n public static void main(String[] args) {\n String serviceName = "YourServiceName"; // 替换为你要检测的服务名称\n\n try {\n Process process = Runtime.getRuntime().exec("sc query " + serviceName);\n BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));\n\n String line;\n while ((line = reader.readLine()) != null) {\n if (line.trim().startsWith("STATE")) {\n String[] state = line.split(":");\n String serviceState = state[1].trim();\n\n if (serviceState.equalsIgnoreCase("RUNNING")) {\n System.out.println("Service is running.");\n } else {\n System.out.println("Service is not running.");\n }\n\n break;\n }\n }\n\n reader.close();\n process.destroy();\n\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n}\n\n\n请将YourServiceName替换为你要检测的服务名称。然后运行这个Java程序,它将输出服务是否正在运行。


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

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