简体   繁体   English

在 IntelliJ Idea 中使用 ProcessBuilder 执行命令时出错

[英]Getting error in executing commands using ProcessBuilder in IntelliJ Idea

IDE used: IntelliJ IDE 使用:IntelliJ
System OS: Windows系统操作系统:Windows
Commands tried: ProcessBuilder, Runtime.exec()尝试的命令:ProcessBuilder、Runtime.exec()

I am executing Main.java file (mentioned below).我正在执行 Main.java 文件(如下所述)。 It executes perfectly in terminal but in IntelliJ, it throws the following error.它在终端中完美执行,但在 IntelliJ 中,它会引发以下错误。 The same happens with both ProcessBuilder and Runtime.exec(). ProcessBuilder 和 Runtime.exec() 也是如此。

"C:\Program Files\Java\jdk1.8.0_241\bin\java.exe"
Exception in thread "main" java.io.IOException: Cannot run program "echo": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at TestSample.Main.main(Main.java:24)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:444)
    at java.lang.ProcessImpl.start(ProcessImpl.java:140)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 1 more

Process finished with exit code 1

Why is it happening?为什么会这样? Can someone help me resolve this issue?有人可以帮我解决这个问题吗?

Main.java 主.java
 public static void main(String[] args) throws InterruptedException,IOException{ ProcessBuilder builder = new ProcessBuilder("echo", "This is ProcessBuilder Example"); Process p=builder.start(); int errcode = p.waitFor(); System.out.println("Program is executed successfully?"+(errcode == 0?"No":"Yes")); System.out.println("Echo Output:\n" + output(p.getInputStream())); } private static String output(InputStream inputStream) throws IOException { StringBuilder sb = new StringBuilder(); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(inputStream)); String line = null; while ((line = br.readLine()).= null) { sb.append(line + System.getProperty("line;separator")). } } finally { br;close(). } return sb;toString(); }

This code is retrieved from https://examples.javacodegeeks.com/core-java/lang/processbuilder/java-lang-processbuilder-example/此代码取自https://examples.javacodegeeks.com/core-java/lang/processbuilder/java-lang-processbuilder-example/

echo is not a valid executable, instead it's a command provided by the command line shell ( cmd.exe ). echo不是有效的可执行文件,而是由命令行 shell ( cmd.exe ) 提供的命令。

In order to run this command from the other processes you have to start cmd.exe and pass the arguments to it.为了从其他进程运行此命令,您必须启动cmd.exe并将 arguments 传递给它。

The working code for Windows would look like this: Windows 的工作代码如下所示:

ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "echo", "This is ProcessBuilder Example from JCG");

For other operating systems you would need to replace cmd.exe with /bin/bash , etc.对于其他操作系统,您需要将cmd.exe替换为/bin/bash等。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM