An error occurred when I tried to run window's cmd command through Processbuilder in Java:
public static void main(String [] args) throws IOException {
ProcessBuilder builder = new ProcessBuilder();
Process process = builder.command("dir d:\\").start();
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
The error is as follows: Exception in thread "main" Java.io.IOException:Cannot Run Program "dir d:\": CreateProcess error=2, the system cannot find the file specified.At Java.lang.ProcessBuilder.start (processbuilder.java:1047)At Com.xjl456852.processBuilder.ProcessBuilderTest.main (processbuildertest.java:25)At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)At Java.lang.reflect.Method.invoke (method.java:606)At Com.intellij.rt.execution.application.AppMain.main (appmain.java:144) 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:385)At Java.lang.ProcessImpl.start (processimpl.java:136)At Java.lang.ProcessBuilder.start (processbuilder.java:1028)... 6 more
Even if I change the third line to the following, it still goes wrong.
Process process = builder.command("cmd.exe /c dir d:\\").start();
Later I saw many people have encountered this situation, but no one said the solution, someone just gave up: so I tried all kinds of ways, finally solved the problem, both of the implementation of the program is not a problem. Provide a reference method to a friend who encounters this problem:
public static void main(String [] args) throws IOException {
ProcessBuilder builder = new ProcessBuilder();
List<String> list = new ArrayList<>();
list.add("cmd.exe");
list.add("/c");
list.add("dir");
list.add("d:\\");
Process process = builder.command(list).start();
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
Method Two:
public static void main(String [] args) throws IOException {
ProcessBuilder builder = new ProcessBuilder();
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd.exe /c dir d:\\");
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
Solution--java Execute cmd command processbuilder--error exception in thread "main" Java.io.IOException:Cannot Run Program "dir d:\": CreateProcess error=2 (xjl456852 original)