wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Shell script from a java application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Shell script from a java application" Watch "Shell script from a java application" New topic
Author

Shell script from a java application

Sayantan Auddy
Ranch Hand

Joined: Dec 18, 2008
Posts: 35
My requirement is as follows:
There is a java desktop application which runs a shell script. The shell script does an ssh to connect to a remote server and does some processing. The output of the shell script will be shown in the java application.

I started out with the following code but the program is getting hung. Any pointers regarding where I am going wrong will be highly appreciated.





Sayantan Auddy,SCJP 5.0
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I can see three problem -

1) You need to send an end-of-line (a \n) after the command and to close() the stdin stream.
2) You need to process stderr because if the stderr buffer fills before the process has completed then you will get a deadlock. This should be done in a separate thread.
3) You need to process stdout in a separate thread since again it can cause deadlock if it's buffer becomes full.

You would do well to read the 4 sections of the traps article and implement ALL the recommendations.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Sayantan Auddy
Ranch Hand

Joined: Dec 18, 2008
Posts: 35
Thanks a lot James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shell script from a java application
 
Similar Threads
issue in passiing variable to shell script
Java file.mkdir() does not work even after chmod set to 777
Return value from main
shell script API
How to start my java application by shell script??