Solution--java Execute cmd command processbuilder--error exception in thread "main" Java.io.IOException:Cannot Run Program "dir d:\": CreateProcess error=2 (xjl456852 original)

Source: Internet
Author: User

An error occurred when I tried to run window's cmd command through Processbuilder in Java:
  
 
  1. public static void main(String [] args) throws IOException {
  2. ProcessBuilder builder = new ProcessBuilder();
  3. Process process = builder.command("dir d:\\").start();
  4. InputStream inputStream = process.getInputStream();
  5. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
  6. String line = null;
  7. while((line = br.readLine()) != null) {
  8. System.out.println(line);
  9. }
  10. }
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.
 
   
  
  1. 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:
  
 
  1. public static void main(String [] args) throws IOException {
  2. ProcessBuilder builder = new ProcessBuilder();
  3. List<String> list = new ArrayList<>();
  4. list.add("cmd.exe");
  5. list.add("/c");
  6. list.add("dir");
  7. list.add("d:\\");
  8. Process process = builder.command(list).start();
  9. InputStream inputStream = process.getInputStream();
  10. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
  11. String line = null;
  12. while((line = br.readLine()) != null) {
  13. System.out.println(line);
  14. }
  15. }
Method Two:
  
 
  1. public static void main(String [] args) throws IOException {
  2. ProcessBuilder builder = new ProcessBuilder();
  3. Runtime runtime = Runtime.getRuntime();
  4. Process process = runtime.exec("cmd.exe /c dir d:\\");
  5. InputStream inputStream = process.getInputStream();
  6. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
  7. String line = null;
  8. while((line = br.readLine()) != null) {
  9. System.out.println(line);
  10. }
  11. }




Solution--java Execute cmd command processbuilder--error exception in thread "main" Java.io.IOException:Cannot Run Program "dir d:\": CreateProcess error=2 (xjl456852 original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.