• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

execute shell script from Jsp or Servlet not Working

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you making assumptions about the working directory?
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's really not much evidence that he's paying mush attention at all to the replies.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this not possible calling though jsp.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 41
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic