Wednesday, July 25, 2012

Running a Unix shell script from JSP on Tomcat

Let's start with the simplest version of Runtime.exec()

if I do:

<%
java.lang.Runtime.getRuntime().exec("/home/soa/proton/runme.sh", new String[] {"MYVAR=FANCULON"});

%>


you pass to the script the System variable MYVAR with the value FANCULON

runme.sh contains:
echo $MYVAR > myvar.log

myvar.log ends up in Tomcat bin directory, which is assumed as "current directory" for the script.


I can do better by specifying the current directory for the process with this other version of exec.

So if I do

java.lang.Runtime.getRuntime().exec("/home/soa/proton/runme.sh", new String[] {"MYVAR=FANCULON"}, new java.io.File("/home/soa/proton/"));

the myvar.log is written in /home/soa/proton/

No comments: