jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes problems invoking a shell script from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problems invoking a shell script from a servlet" Watch "problems invoking a shell script from a servlet" New topic

problems invoking a shell script from a servlet

rahul takkallapally

Joined: Oct 03, 2003
Posts: 6
i am on a unix machine , i have a servlet which is invoked from an applet , this servlet has to invoke a shell script , for invoking the shell script i have written something like this..

void runCommand1() {
try {
String command = "/usr/filetesting/testing.sh";
Process process = Runtime.getRuntime().exec(command);
InputStream istream = process.getInputStream();
InputStreamReader ireader = new InputStreamReader(istream);
BufferedReader breader = new BufferedReader(ireader);
stringValue1=" testing the end of the runcommand block";
catch (IOException e) {
System.out.println("" + e);
and the shell script has only one command mkdir anirudh
there are no exceptions comming up , nor is the directory being created
please tell me what could i be doing wrong.....
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Well, if the command really looks like "mkdir anirudh", then the problem could just be that the current directory isn't whatever you're assuming it to be. There's nothing in your code that would detect whether this mkdir command failed.
The first thing to try: hard code an absolute path like "/tmp/anirudh" into your script and see if that directory appears. If so, then you know the problem is just that the current directory wasn't what you thought it was.

[Jess in Action][AskingGoodQuestions]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13025
If the command is generating an error report it would be on the error stream - you need to provide for reading it also.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Right, you may be creating the directory, but in another location. You can find it by using the unix find command i.e. .
But you probably want to change your shell script to something like this:
or something that returns a message code. This will create the directory only if it doesn't exist, otherwise you will generate an error message if you run mkdir twice and the directory already exists. Then you can check the error stream.
I agree. Here's the link: http://aspose.com/file-tools
subject: problems invoking a shell script from a servlet
It's not a secret anymore!