File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes execute shell script from Jsp or Servlet not Working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "execute shell script from Jsp or Servlet not Working" Watch "execute shell script from Jsp or Servlet not Working" New topic
Author

execute shell script from Jsp or Servlet not Working

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi All,

I am executing sh file from

1) /webapps/app/WEB-INF/classes/test.java and test.class --> test.java calling sh file
so if i run from unix server : $java test
It calls sh file and sh files just writes some echo text message to data.txt
2) But i call same from

http//localhost:8080/app/MyServlet

or http://localhost:8080/index.jsp

jsp calls beans and bean calls sh file -- this is not wokring...i am all log prints but sh file not getting called and data.txt file is blank.

what could be the issue calling sh file from jsp using beans or Servlet.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Are you making assumptions about the working directory?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
yes

jsp is in - /webapps/app/index.jsp


Beans in some package inside classes

test.java is inside classes only which works fine. But servlet is in classes path and jsp file call using javabeans not working.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Prabhat Ranjan wrote:
test.java is inside classes only which works fine. But servlet is in classes path and jsp file call using javabeans not working.


Without any view at all of your code what can one say except that you are doing something wrong ! There is nothing special about running shell scripts using either ProcessBuilder or Runtime.exec() from within a Servlet/JSP but you must still follow the recommendations in all the sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html .

My guesses are that you are assuming a working directory that does not apply or an environment (PATH maybe) that does not apply. If you have logged the Process 'stdout' and 'stderr' then it should be easy to diagnose.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Prabhat Ranjan wrote:yes

jsp is in - /webapps/app/index.jsp


Beans in some package inside classes

test.java is inside classes only which works fine. But servlet is in classes path and jsp file call using javabeans not working.


None of this has any bearing on the working directory.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Why this will not work , give me the valid reason.

or let me know if this is bad idea to call sheel script from jsp and servlet in web application.

Then we need to remove the idea as this is not poosbile with somthing environment or Technology limitation.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

Let's look at it from a different point of view. I assume you are calling Runtime.exec(); when you do that, what is the value you are passing to for the command string? Most likely that value is not correct, you really need to be passing a full path, not a relative path because relative paths are really difficult to get correct in a web app environment.


JBoss In Action
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Prabhat Ranjan wrote:Why this will not work , give me the valid reason.

or let me know if this is bad idea to call sheel script from jsp and servlet in web application.

Then we need to remove the idea as this is not poosbile with somthing environment or Technology limitation.


It can work but you have to do it right. Did you read my response? Did you read the 'traps' article I cited? Obviously not.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi All,

This is the servlet Code: and i ran by http://localhost:8080/app/HelloWorld sh file is not being called.


}


Servlet Class is complied and inside the classes directory.

however if i write the same code inside Java main Program in classes it works.

I have test.sh file in side the classes.
$java test
It writes the echo message in data.txt file. But nothing works if i use to call the same method fropm Web application(jsp/Servlet/Java Beans).

It is not working.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Prabhat Ranjan wrote:
It is not working.


I'm not surprised since there is no evidence in your code that you have read the 'traps' article.

You can lead a horse to water but ....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

There's really not much evidence that he's paying mush attention at all to the replies.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I think this not possible calling though jsp.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

There is no such thing as a "current directory" in J2EE.

Yes, there's the concept, but in actuality, the server can asynchronously change directories without notice at any time. And I do mean any time - if some other thread in some other webapp or in the server itself does a "change directory" command, your "current directory" will literally change in the middle of whatever your app is doing.

And there's not a thing you can do about it. Not even use "synchronized".

For this reason, you should always use absolute paths when referencing files and executables in JSP/servlet code. You cannot trust the "current directory" to be where you hope it is, so relative paths will fail or at least be very, very unreliable. Even if it "works" for years, Murphy's Law predicts that it will fail when it's least convenient.

If you do not wish to hard-code absolute paths in your webapp, consider making them injectable application options using JNDI or a database or something like that. That's what I do.


Customer surveys are for companies who didn't pay proper attention to begin with.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I have now added the excat path but still its not working

sh /VM_APP/catalina_base/webapps/HealthCheck/WEB-INF/classes/test.sh
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

I recommend capturing stdout and stderr when you execute this script to see what messages are coming back.

Also, the lack of a current directory also applies to the script itself as well as the script location, so if the script is writing out a data.txt file, you either need to include an absolute path to tell where data.txt is going to be written or have the script execute a "cd" command to set a known current directory in which to output the data.txt file.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Prabhat Ranjan wrote:I think this not possible calling though jsp.


How many times do we have to tell you that it is possible. You are just doing it wrong. Why are you ignoring pretty much all the advice you are given? Why have you not studied the 'traps' article and implemented ALL the recommendations?

I give in. Bye.
Joseph Swager
Ranch Hand

Joined: Feb 04, 2010
Posts: 41

I wonder if permissions are even set correctly to execute the script. I'm just saying sometimes its the simplest things that cause the most frustration.


"Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do." -- Steve Jobs
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Joseph Swager wrote:I wonder if permissions are even set correctly to execute the script. I'm just saying sometimes its the simplest things that cause the most frustration.


In this case I doubt it. I know from bitter experience the first time I used Runtime.exec() many many years ago and frequent usage of Runtime.exec() and ProcessBuilder since then that every thing the OP has published indicates that the problem is to do with the environment (or lack of it) that he is trying to run the script under. Until he reads the 'traps' article and implements ALL the recommendations ( at the moment he is falling for at least 3 of the traps ) he is unlikely to get it to work.
Joseph Swager
Ranch Hand

Joined: Feb 04, 2010
Posts: 41

So true Richard. After reading his code posted it doesn't follow any the suggestions. I'd tell him how to do it with groovy which makes executions like this a bit easier, but I'm sure that wouldn't be followed either. Also, as he ever heard of Restful web services. Expose the code as an include of an URL in the jsp page. There is so many ways he could do this.
 
wood burning stoves
 
subject: execute shell script from Jsp or Servlet not Working