I am interested if anyone has an idea on how to run unix commands without using runtime or ProcessBuilder in a java application
3 Answers
Well you can use DefaultExecutor from Apache Commons Exec library to execute commands but it internally uses java.lang.Runtime
and java.lang.Process
.
I would suggest you to use this library over Runtime because Apache Command Execution APIs are more sophisticated, and provide all and more features than Java Runtime. It also handles exit Values.

- 16,024
- 8
- 58
- 85
-
Does it handle the hang issue as detailed in this comment - http://stackoverflow.com/a/5483880/537503 ? – Andy Dufresne Jul 10 '14 at 04:18
There are quite a few possible ways; but I can't call any of them desirable without understanding your motivation.
For example, write a program in C or Perl or whatever language and have it listen on a socket. Your java program can then connect to the socket and send a message with the name of the program to spawn, arguments etc. The receiver program can go ahead and spawn this.

- 132
- 1
- 2
-
right so the idea for this is to create java server app which responds to basic commands like ls, pwd, cd etc. – tosi Aug 16 '12 at 13:31
There are two ways to execute Unix commands:
- You can use
java.lang.Runtime
andjava.lang.Process
- You can use JNI/JNA to access shared libraries. So you could use this to access the OS level commands to create a process (
system()
andexec*()
on Unix).
The second approach isn't recommended: It's hard to get right and eventually, you will end up with something that has exactly the same functionality as Runtime.exec()
If you only need the functionality, then you can implement the commands again in Java. For example, to implement ls
, use File.listFiles()

- 321,842
- 108
- 597
- 820