java中的linux shell脚本执行(wget)

java中的linux shell脚本执行(wget),java,linux,shell,Java,Linux,Shell,我用java制作了服务器监控应用程序 I exec shell脚本(wget--no cache-t1-t1000--spider ServerURL 2>&1 | grep'HTTP') 我得到了结果 但是在java exec shell脚本中 我没有得到结果 源代码 String command = "wget --no-cache -t 1 -T 1000 --spider " + monitering_url +" 2>&1 | grep 'HTTP'"; shellCmd

我用java制作了服务器监控应用程序

I exec shell脚本(wget--no cache-t1-t1000--spider ServerURL 2>&1 | grep'HTTP')

我得到了结果

但是在java exec shell脚本中

我没有得到结果

源代码

String command = "wget --no-cache -t 1 -T 1000 --spider " + monitering_url +" 2>&1 | grep 'HTTP'";
shellCmd(command);
public static void shellCmd(String command) throws Exception {
          Runtime runtime = Runtime.getRuntime();
          Process process = runtime.exec(command);
                    //Thread.sleep(5000);
          InputStream is = process.getInputStream();
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line;
          while((line = br.readLine()) != null) {
              System.out.println(line);
          }
      }
监视\u url=serverURL

我要打印结果

救救我

您的命令是shell脚本,必须传递给shell进行解释:

Process process = runtime.exec(new String[] {"/bin/sh", "-c", command});

注意使用数组将参数传递给shell进程。没有它,命令字符串将在传递给解释器之前被标记,这不是您想要的。

这将需要shell进行解释(用于管道和重定向)。调用包含命令的脚本或将它们传递给解释器。我解决了问题,非常感谢!!