• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Execute windows command

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yesterday I started a thread and wished for it's deletion and poof, it was gone.

I wanted to follow up with my findings from experimenting with the Process Class and executing windows commands from inside a Java program.

I read the article about "When Runtime.exec() won't" but had a few comments about the info in that article. For starters, the article attempts to capture the stream from issuing a "javac" command. However, how does one know in advance that certain commands require an ErrorStream and certain commands only need the standard InputStream?

The article suggested the example of issuing "javac" was mediocre because the exitVal was = 2, however they never show you how to issue that command and get an exitVal=0

To me, I understand this to mean that issuing: "dir" produces an InputStream and issuing "javac" produces an ErrorStream. You can't output the stream from "javac" without the ErrorStream. When I try, the process hangs and no output is written.

Playing around with other windows system commands produces other error codes, such as exitVal=1 when you issue: "cmd.exe /c dir /?" You can output the stream using p.getInputStream, but the exitVal=1, which according to MSDN means that it is an "incorrect function".

Does this mean that capturing the stream from: "cmd.exe /c dir /?" was not implemented correctly, because the exitVal != 0 ?

Thoughts?

Sample code:


 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When using Process, it's a good practice to read both the getInputstream (i.e, the stdout of the called process) and getErrorStream (i.e, the stderr of the called process).

If you have no special reason to process them separately, then a better approach is to use the ProcessBuilder class. Its redirectErrorStream() method allows the stderr of the called process to be redirected to its stdout; so only the getInputStream() needs to be read to get the contents of both streams.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"dir /?" indeed reports that it exits with an error. Perhaps this is not incorrect implementation but choice - after all, dir does not list anything.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic