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
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.
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.
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.
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.
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.
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 ....
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.
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.
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 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.