I'm trying to call a perl script from a java servlet on tomcat 7. I have set up the context.xml and the web.xml so I can run a .pl file by going to http://localhost:8080/test/cgi-bin/test.pl and I can also run perl from directly within java like so:
String[] cmdArr = new String[]{"perl", "-e", "print \"Content-type: text/html\n\n\";$now = localtime();print \"<h1>It is $now</h1>\";"};
if (cmdArr != null) {
Process p = null;
Runtime rt = Runtime.getRuntime();
try {
p = rt.exec(cmdArr); // throws IOException
returnValue = p.waitFor(); // throws InterruptedException
}
catch (IOException xIo) {
throw new RuntimeException("Error executing command.",xIo);
}
catch (InterruptedException xInterrupted) {
throw new RuntimeException("Command execution interrupted.",xInterrupted);
}
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader stdout = null;
stdout = new BufferedReader(isr);
String line = null;
try {
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException xIo) {
throw new RuntimeException("Error reading process output", xIo);
}
}
and this works fine, but if I try and refer to a .pl script that is in my /WEB-INF/cgi folder by replacing the:
String[] cmdArr = new String[]{"perl", "-e", "print \"Content-type: text/html\n\n\";$now = localtime();print \"<h1>It is $now</h1>\";"};
with something like:
String cmdArr = "/WEB-INF/cgi/test.pl";
or
String cmdArr = "/cgi-bin/test.pl";
I keep getting this error:
java.io.IOException: Cannot run program "/WEB-INF/cgi/test.pl": error=2, No such file or directory
I'm guessing im going wrong with the file path somewhere? Any help would be really appreciated!
UPDATE:
after @hobbs comment I changed to: String[] cmdArr = new String[]{"perl", "/WEB-INF/cgi/test.pl"};
but if I add the following before the .waitFor():
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ( (line = br.readLine()) != null){
System.out.println(line);
}
I get the print:
Can't open perl script "/WEB-INF/cgi/test.pl": No such file or directory
I guess this is back to the original problem?