简体   繁体   English

使用“ Runtime.getRuntime()。exec()”执行命令行

[英]Executing command line using “Runtime.getRuntime().exec()”

I'm having a problem while executing a command line from Java code. 从Java代码执行命令行时遇到问题。
This is the code I use, and under the code are the errors I got. 这是我使用的代码,在代码下是我遇到的错误。
I also tried using ProcessBuilder but I got the same errors. 我也尝试使用ProcessBuilder但出现了相同的错误。

Ps: I am running the program on Windows. 附:我正在Windows上运行该程序。

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        Process compilerProcess = Runtime.getRuntime().exec("clafer -k -m choco clafer.cfr);

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                compilerProcess.getInputStream()));
            String s = null;
            while ((s = stdInput.readLine()) != null) {
             System.out.print(s);        }  
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // To have access to the text generated by the compiler.


}

}

The error: 错误:

java.io.IOException: Cannot run program "echo": CreateProcess error=2, the 
  specified file is not found
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at eqe.Test.main(Test.java:12)
Caused by: java.io.IOException: CreateProcess error=2, the specified file 
is not found
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

I found the solution to the problem. 我找到了解决问题的办法。 I simply put the command line into new String[] and worked. 我只是将命令行放入新的String []中并开始工作。
Here is the solution for using to command line that allows you to execute long command line. 这是使用命令行的解决方案,允许您执行较长的命令行。

Process compilerProcess = Runtime.getRuntime().exec(new String[]{"clafer","-k","-m","choco","clafer.cfr" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Runtime.getRuntime()。exec()执行查找命令时出错 - Error executing find command through Runtime.getRuntime().exec() Java Runtime.getRuntime()。exec仅执行第一行 - Java Runtime.getRuntime().exec Executing only first line Runtime.getRuntime()。exec()的输出与执行命令行的结果不同 - Runtime.getRuntime().exec() output is not the same result from executing a command line Runtime.getRuntime()。exec()输出与直接执行命令行程序不同 - Runtime.getRuntime().exec() output differ from executing a command line program directly 在Mac上使用Java Runtime.getRuntime()。exec执行终端命令时,权限被拒绝例外 - Having permission denied exception when executing terminal command using Java Runtime.getRuntime().exec on Mac 用户在使用 Runtime.getRuntime().exec(command) 时输入到命令行; - User input to the command line when using Runtime.getRuntime().exec(command); 问题执行Runtime.getRuntime()。exec AWK - Issue executing Runtime.getRuntime().exec AWK Runtime.getRuntime()。exec(),执行Java类 - Runtime.getRuntime().exec(), executing Java class Runtime.getRuntime()。exec-执行osql查询 - Runtime.getRuntime().exec - Executing osql query 使用Runtime.getRuntime()。exec(String [])在Java中运行Windows命令 - Running Windows Command in Java Using Runtime.getRuntime().exec(String[])
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM