0

I have following code:

ProcessBuilder pb = new ProcessBuilder("dir"); // or cat in linux
Process p = pb.start();
p.waitFor();
String result = IOUtils.toString(p.getInputStream(), "UTF-8");
System.out.println(result);

It throws

java.io.IOException: Cannot run program "dir": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) ~[na:1.8.0_111]

I understand that I can write something like this:

ProcessBuilder pb = new ProcessBuilder("test.bat");

and inside test.bat you can write

dir

But it returns:

D:\nsd-rest>dir
 Volume in drive D is SECOND
 Volume Serial Number is CE52-8896

 Directory of D:\nsd-rest

03/24/17  15:53    <DIR>          .
03/24/17  15:53    <DIR>          ..
03/24/17  12:00               249 .gitignore
03/24/17  16:54    <DIR>          .idea
03/24/17  15:01    <DIR>          .mvn
03/24/17  12:00             7,058 mvnw
03/24/17  12:00             5,006 mvnw.cmd
03/24/17  15:53             6,265 nsd-rest.iml
03/24/17  15:52             1,993 pom.xml
03/24/17  15:01    <DIR>          src
03/24/17  15:19    <DIR>          target
03/24/17  16:52                 3 test.bat
               6 File(s)         20,574 bytes
               6 Dir(s)  587,412,844,544 bytes free

Row

D:\nsd-rest>dir

is redundant.

When I invoke bat file I feel that I do something incorrect.

Can you provide correct solution?

P.S. instead of dir can be any executable file.

7
  • Just a thought, Java may not have access to the same environment variables as your command prompt. (e.g. %PATH%) Commented Mar 24, 2017 at 14:02
  • @CraigR8806 Is there way to add dir to path ? Commented Mar 24, 2017 at 14:05
  • First argument in commands for process builder is treated as executable file, from what I can see, there's no exception to that rule. What you actually want is execute command line process with passing "dir" as first argument to that process. Commented Mar 24, 2017 at 14:10
  • [This may help] (stackoverflow.com/questions/19621838/…) Commented Mar 24, 2017 at 14:10
  • waitFor() returns after the process has finished. Call it after you are done reading from the InputStream. Commented Mar 24, 2017 at 14:30

1 Answer 1

1

There's no dir executable actually - e.g. you can't find dir.exe anywhere on windows. This is pseudo-command which exist in cmd.exe executable context only.

If you want to get "dir" output - you can simply run cmd.exe /c dir

Also there's bug in your program:

p.waitFor();
String result = IOUtils.toString(p.getInputStream(), "UTF-8");

If program output is large enough and wouldn't fit into output buffer, p.waitFor() would never end - because buffer is overflown and nobody is reading it. I'd suggest swap these lines(or even better - read couple articles about interprocess IO, that's related to everything not to java world only).

Sign up to request clarification or add additional context in comments.

9 Comments

That, of course, means it will only work on Windows systems.
I'd never run dir directly, but rather iterate through File.list() or something similar but that wasn't the original question.
Try new ProcessBuilder("cmd","/c","dir")
It actually supposed to be new ProcessBuilder("cmd", "/c", "dir")
cmd /? is your best friend.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.