wood burning stoves*
The moose likes Java in General and the fly likes Running Shell Script from Java Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running Shell Script from Java Program" Watch "Running Shell Script from Java Program" New topic
Author

Running Shell Script from Java Program

SAM KUMAR
Greenhorn

Joined: Jan 02, 2002
Posts: 25
Hi,
I am trying to call a shell script from Java and I am not sure how can I find out whether the script ran sucessfully or not.
Shell Script:ftpscript.sh
ftp -n testServer << EOD
user userName Password
bin
cd /Reports
put rpt1.csv
quit
Java Code Snippet
-----------------
Runtime myRuntime = Runtime.getRuntime();
String cmd = "command ftpscript.sh";
Process ps = myRuntime.exec(cmd);
ps.waitFor();
ps.destroy();
System.out.println("Exit Status: " + ps.exitValue() + " " + ps.toString());
Lets say user password is invalid, in that case ftpscript.sh will unable to ftp the file but from Java point of view I am getting Exit Status as ZERO (Prociess.exitValue()).
Any ideas how to know whether FTP script was sucessful or not.
Thanks.
SK
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Yes -- rewrite the ftp script to return an error status when it fails!
Otherwise you have to use the javax.sentience package (JSR-10023) which won't be ready for another 20 years or so.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Yes -- rewrite the ftp script to return an error status when it fails! You might have the script test for the existence of the file by running the ftp "ls" command on the server, and piping the result through "grep". Be careful because grep returns non-zero on success, zero on failure!

Otherwise you have to use the javax.sentience package (JSR-10023) which won't be ready for another 20 years or so.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running Shell Script from Java Program