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
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.
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.
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.
Joined: Oct 04, 2006
I have now added the excat path but still its not working
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.
Joined: Aug 27, 2012
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 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
Joined: Aug 27, 2012
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.
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.