Take a look at ProcessBuilder
. After you set up ProcessBuilder
and ProcessBuilder
start
, you will have a Process
handle with which you can feed and read.
Here is a snippet you can start:
ProcessBuilder pb = new ProcessBuilder("/bin/bash"); Process proc = pb.start(); // Start reading from the program final Scanner in = new Scanner(proc.getInputStream()); new Thread() { public void run() { while (in.hasNextLine()) System.out.println(in.nextLine()); } }.start(); // Write a few commands to the program. PrintWriter out = new PrintWriter(proc.getOutputStream()); out.println("touch hello1"); out.flush(); out.println("touch hello2"); out.flush(); out.println("ls -la hel*"); out.flush(); out.close();
Conclusion:
-rw-r--r-- 1 aioobe aioobe 0 2011-04-08 08:29 hello1 -rw-r--r-- 1 aioobe aioobe 0 2011-04-08 08:29 hello2
source share