Java Runtime.getRuntime().exec 中的命令不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21908645/
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-08-13 11:22:21  来源:igfitidea点击:

Command in Runtime.getRuntime().exec not working

javacommand-linejavac

提问by kajarigd

I have a java file named MemoryComparison.javakept in the folder D:\Documents\CodeAnalysis\project_analysis_RG\CodeAnalysis\input\master\Kajari_G

我有一个名为的 java 文件MemoryComparison.java保存在文件夹中D:\Documents\CodeAnalysis\project_analysis_RG\CodeAnalysis\input\master\Kajari_G

I have another folder D:\Documents\CodeAnalysis\output\where I want to store the output of the above java file after execution.

我有另一个文件夹D:\Documents\CodeAnalysis\output\,我想在执行后存储上述 java 文件的输出。

In the Java program MemoryComparison.java I have not given any package name.

在 Java 程序 MemoryComparison.java 中,我没有给出任何包名。

Now, I am compiling and executing the above java program from another java program named ReadFilesInFolders.java. Below is the code snippet:

现在,我正在从另一个名为ReadFilesInFolders.java. 下面是代码片段:

try {
    Runtime.getRuntime().exec("javac input\master\Kajari_G\MemoryComparison.java");
    Runtime.getRuntime().exec("java -cp input\master\Kajari_G\ MemoryComparison > output\output1.txt");
     } catch (IOException e) {
    e.printStackTrace();
     }

Now, the first exec is working fine and the .class file is getting generated in the same folder, where MemoryComparison.java is. But it seems the second exec is not working as no output1.txt is getting created.

现在,第一个 exec 工作正常,并且 .class 文件正在同一文件夹中生成,即 MemoryComparison.java 所在的文件夹。但似乎第二个 exec 不起作用,因为没有创建 output1.txt。

But when I am running the above two lines from the command prompt, everything is working fine and the output1.txt is getting created.

但是当我从命令提示符运行上述两行时,一切正常,并且正在创建 output1.txt。

Can you please help me with this!

你能帮我解决这个问题吗!

回答by ???v?т?

You need to wait for the first process to complete before you execute the next one.

在执行下一个过程之前,您需要等待第一个过程完成。

Process p = Runtime.getRuntime().exec("javac ...");
p.waitFor();
p = Runtime.getRuntime().exec("java ...");
p.waitFor();

回答by Chandrayya G K

Try this code.

试试这个代码。

    Process proc = null;
    try {
        String command = "javac input\master\Kajari_G\MemoryComparison.java";
        // Similarly for this: "java -cp input\master\Kajari_G\ MemoryComparison > output\output1.txt" also           
        proc = Runtime.getRuntime().exec(new String[] { "/bin/sh"//$NON-NLS-1$
                , "-c", command });//$NON-NLS-1$
        if (proc != null) {
            proc.waitFor();
        }
    } catch (Exception e) {
        //Handle
        return;
    }


Edit

编辑

On Windows use **cmd** exepath instead of /bin/sh. Check whether -c command line parameter was supported in **cmd** exe.

在 Windows 上使用**cmd** exepath 而不是/bin/sh. 检查 .c 文件中是否支持 -c 命令行参数**cmd** exe

回答by Ankit Kumar

You need to use ProcessBuilderto redirect... Generally a good practice to use ProcessBuilder API of Java

您需要使用ProcessBuilder重定向...通常是使用 Java 的 ProcessBuilder API 的好习惯

Here is an example fromthat starts a process with a modified working directory and environment, and redirects standard output and error to be appended to a log file:

这是一个示例该示例使用修改后的工作目录和环境启动进程,并将标准输出和错误重定向到附加到日志文件中:

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == Redirect.PIPE;
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;