2017-07-22 6 views
0

processidがポート番号で実行されています。javaを使用してLinuxの特定のポートでプロセス情報を取得する方法

else if(OS.contains("linux")){ 
    try{ 
     JOptionPane.showMessageDialog(null, "in linux "); 
     Runtime rt = Runtime.getRuntime(); 
     Process proc = rt.exec("sudo netstat -nlp | grep :9090"); 

     BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(proc.getInputStream())); 
     String s = null; 
     StringBuilder sb=new StringBuilder(); 
     while ((s = stdInput.readLine()) != null) { 
      sb.append(s); 
      System.out.println(s); 
     } 
} 

をしかし、このコマンドが実行取得されていません。その後、私はこれは私が試したものです。だからプロセスprocessidを使用していることを殺したいです。

したがってprocess idのプロセスは、9090ポートで実行されていますか。

答えて

0

私はそれはあなたの

else if (OS.contains("linux")) { 
     try { 

      Runtime rt = Runtime.getRuntime(); 
      Process proc = rt.exec("netstat -nlp | grep :9090"); 

      BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
      String s = null; 
      StringBuilder sb = new StringBuilder(); 
      String sc = null; 
      while ((s = stdInput.readLine()) != null) { 
       if (s.contains("9090")) { 
        sb.append(s); 
        System.out.println(s); 

        String process = s.trim(); 
        int index = process.lastIndexOf(" "); 
        int to = process.indexOf("/"); 

        sc = s.substring(index, to); 

       } 

       rt.exec("kill -9 " + sc); 
      }} 
を助けることを願っています。この実行中のコードを試してみてください
関連する問題