8

I am interested if anyone has an idea on how to run unix commands without using runtime or ProcessBuilder in a java application

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
tosi
  • 1,873
  • 2
  • 18
  • 38

3 Answers3

5

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.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

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.

Ambar
  • 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
-1

There are two ways to execute Unix commands:

  1. You can use java.lang.Runtime and java.lang.Process
  2. 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() and exec*() 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()

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820