• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to execute shell(unix) through a servlet.  RSS feed

 
kajal khanna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to execute a shell through the servlet .. but it is not working ...
i have used it in the following way ..
.
.
Process p=null;
Runtime r1=Runtime.getRuntime();
try{
p=r1.exec("sh abc");
p.waitFor();
}
catch(Exception e){}
.
.
but it is not executing the shell .. can somebody help me ...
 
Mohamed Yousuff
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I hope you are using unix os. 'sh abc' is not a valid shell command. Check for other commands like 'ls'. Note that you cannot see the output of the program in the screen where you are executing. The output can be obtained as follows
BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream()) ;
String str ;
while( (str=br.readLine() != null )
System.out.println( str ) ;

In the above code, in the constructor of InputStreamReader, p is the process object that you obtained using runtime.exec() function.
This will solve your problem

Originally posted by kajal khanna:
I'm trying to execute a shell through the servlet .. but it is not working ...
i have used it in the following way ..
.
.
Process p=null;
Runtime r1=Runtime.getRuntime();
try{
p=r1.exec("sh abc");
p.waitFor();
}
catch(Exception e){}
.
.
but it is not executing the shell .. can somebody help me ...


------------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!