• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to exxcute linux commands from java app  RSS feed

 
chandhana
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all,
i have a small doubt,
i want to develop 1 java application at server side.
from tat application how i pass linux commands to a client application.
plz analyse my problem & give a correct sugession.
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use the Runtime.exec() methods to execute commands.
For example:
Runtime.getRuntime().exec("ls -l")
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to capture your linux command output then you'll need to open a file descriptors on the Process object returned from exec() method to send it input or obtain its output:
example:
import java.io.*;
class executeLinuxCmd {
public static void main(String [] args) {
int ch;
try {
Process p = Runtime.getRuntime().exec("ls -ltr");
InputStreamReader isr = new InputStreamReader(p.getInputStream());
while ((ch = isr.read()) != -1) {
System.out.print((char)ch);//Do not use println() method .. instead use print()
}
} catch (IOException ioe) {
}
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!