• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running system commands  RSS feed

 
Garry Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to get my simple Java program to run a system command, but not getting very far and not understanding why. Here's the code that I'm runing on my Linux (Kubuntu 10.04) system:

Pretty simple, right? The process is returning a value of 2 when it exits, though I have no idea of what this means (although I do know that it isn't the expected 0). I've read the API docs for the Process class where it says that:

"The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts."

I've already tried running a shell script using something similar to the above code and that didn't work. But the 'ls' command is a simple command that I'd expect to work. Is it because, as the API docs say:

"All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream())."

and does this mean that I can't run any system commands that involve redirection?
 
Garry Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved my own problem. Or, rather, Michael C. Daconta of JavaWorld.com did about 10 years ago:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

The answer was in his StreamGobbler class: grab the output stream and take care of it myself. If anyone else has this problem, I hope they find this thread.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Thanks for letting us know the solution to your problem, that might help people who are searching for a solution to the same problem later.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another issue: >/data/mytest.txt should not be part of your command. Operators like >, 2>, <, || and && (yes, these exist for most shells as well!) are handled by the shell that executes the command. They should be translated for use with Process:
- for > you should read from process.getInputStream() and write to the file
- for 2> it's similar except you should read from process.getErrorStream(); the special case 2>&1 (redirect error output to standard output) should be done using ProcessBuilder
- for < you should write to process.getOutputStream()
- for both || and && you should use process.waitFor(); with || skip if it's 0, with && skip if it's not 0
 
Garry Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thanks. I found that out myself. Once I'd worked through the logic, I couldn't understand why it still wasn't working. Then I realised I'd left the '>' in. As soon as I deleted it, the code worked fine.

I'm new to Java as well as to the Ranch so thanks to both of you for your comments.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!