java 从java运行shell命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2460297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:16:18  来源:igfitidea点击:

run shell command from java

javaexec

提问by masay

I am working on an application an have an issue about running shell command from java application. here is the code:

我正在开发一个应用程序,但有一个关于从 Java 应用程序运行 shell 命令的问题。这是代码:

public String execRuntime(String cmd) {

        Process proc = null;
        int inBuffer, errBuffer;
        int result = 0;
        StringBuffer outputReport = new StringBuffer();
        StringBuffer errorBuffer = new StringBuffer();

        try {
            proc = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            return "";
        }
        try {
            response.status = 1;
            result = proc.waitFor();
        } catch (InterruptedException e) {
            return "";
        }
        if (proc != null && null != proc.getInputStream()) {
            InputStream is = proc.getInputStream();
            InputStream es = proc.getErrorStream();
            OutputStream os = proc.getOutputStream();

            try {
                while ((inBuffer = is.read()) != -1) {
                    outputReport.append((char) inBuffer);
                }

                while ((errBuffer = es.read()) != -1) {
                    errorBuffer.append((char) errBuffer);
                }

            } catch (IOException e) {
                return "";
            }
            try {
                is.close();
                is = null;
                es.close();
                es = null;
                os.close();
                os = null;
            } catch (IOException e) {
                return "";
            }

            proc.destroy();
            proc = null;
        }


        if (errorBuffer.length() > 0) {
            logger
                    .error("could not finish execution because of error(s).");
            logger.error("*** Error : " + errorBuffer.toString());
            return "";
        }


        return outputReport.toString();
    }

but when i try to exec command like :

但是当我尝试执行如下命令时:

/export/home/test/myapp -T "some argument"

myapp reads "some argument"as two seperated arguments.but I want to read "some argument"as only a argument. when i directly run this command from terminal, it executed successfully.I tried '"some argument"',""some argument"", "some\ argument"but did not work for me. how can i read this argument as one argument.

myapp 读取"some argument"为两个单独的参数。但我只想读取"some argument"为一个参数。当我直接从终端运行此命令时,它执行成功。我试过'"some argument"', ""some argument"""some\ argument"但对我不起作用。我怎么能把这个论点看作一个论点。

回答by Midhat

I recall that the an overload of exec method provides a parameter for the arguments seperately. You need to use that

我记得 exec 方法的重载分别为参数提供了一个参数。你需要使用那个

Yup. Here is it

对。就这个

public Process exec(String[] cmdarray)
             throws IOException

Just make the command line and all arguments Seperate elements of the String array

只需将命令行和所有参数分开字符串数组的元素

回答by Yasir

first make a string
String cmd="/export/home/test/myapp -T \"some argument\"";
then run cmd in proc

首先创建一个字符串
String cmd="/export/home/test/myapp -T \"some argument\"";
然后在proc中运行cmd