【问题标题】:Java Runtime getting a Garbage option errorJava 运行时收到垃圾选项错误
【发布时间】:2013-11-04 22:08:16
【问题描述】:

我想通过我的 java 应用程序搜索某个进程,以便我可以验证我的应用程序是否正在运行 (Apache)。这是类的简化。

package com.tecsys.sm.test;

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

import com.tecsys.sm.util.FinalValues;

public class ShellRuntimeCommands {
    public static void showApacheProcess(){
        try {
            String command = "ps ax | grep \"/apps/apache/2.4.4/bin/httpd\" | grep -v \"grep\"";
            System.out.println("La commande est: " + command);
            final Process proc = Runtime.getRuntime().exec(command);
            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String line = br.readLine();
            System.out.println("The Error line is "+line);
            String status;
            if(line!=null && line!=""){
                status = FinalValues.STARTED_STATUS;
            }else{
                status = FinalValues.STOPPED_STATUS;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
    ShellRuntimeCommands.showApacheProcess();
    }
}

这是输出:

La commande est: ps ax | grep "/apps/apache/2.4.4/bin/httpd" | grep -v "grep"
The line is ERROR: Garbage option.

问题是我复制粘贴输出的字符串命令并在终端中执行,它工作正常,但它不能通过 java 运行时工作。

【问题讨论】:

    标签: java shell runtime option garbage


    【解决方案1】:

    问题是管道是 shell 的一个特性,所以Runtime.exec 不支持它们。这是解决方法:How to make pipes work with Runtime.exec()?

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2014-03-09
      • 1970-01-01
      相关资源
      最近更新 更多