开发者社区 问答 正文

java代码如何判断服务是否启动?

急求: java代码如何判断服务是否启动?

展开
收起
蛮大人123 2016-03-13 17:33:30 4496 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    public static boolean getProcess() {
      boolean flag = false;
      try {
       Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       InputStream os = p.getInputStream();
       byte b[] = new byte[256];
       while (os.read(b) > 0)
        baos.write(b);
       String s = baos.toString();
       // System.out.println(s);
       if (s.indexOf("smss.exe  ") >= 0) {
        System.out.println("yes ");
        flag = true;
       }
       else {
        System.out.println("no ");
        flag = false;
       }
      } catch (java.io.IOException ioe) {
      }
      return flag;
     }
     
     public static void main(String[] args) {
      if(getProcess()){
       System.out.println("********* ok *********");
      }else{
       System.out.println("********* no *********");
      }
     }
    2019-07-17 19:02:55
    赞同 1 展开评论