Я пытаюсь вызвать Perl-скрипт из java-сервлета на tomcat 7. Я настроил context.xml и web.xml, чтобы запустить файл .pl, перейдя в http://localhost:8080/test/cgi-bin/test.pl, и я также могу запускать Perl прямо изнутри java вот так:
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);
}
}
и это работает нормально, но если я попытаюсь сослаться на сценарий .pl, который находится в моей папке / WEB-INF / cgi, заменив:
String[] cmdArr = new String[]{"perl", "-e", "print \"Content-type: text/html\n\n\";$now = localtime();print \"<h1>It is $now</h1>\";"};
с чем-то вроде:
String cmdArr = "/WEB-INF/cgi/test.pl";
or
String cmdArr = "/cgi-bin/test.pl";
Я все время получаю эту ошибку:
java.io.IOException: Cannot run program "/WEB-INF/cgi/test.pl": error=2, No such file or directory
Я предполагаю, что я где-то ошибаюсь с путем к файлу? Любая помощь могла бы быть полезна!
ОБНОВЛЕНИЕ: после комментария @hobbs я изменился на: String[] cmdArr = new String[]{"perl", "/WEB-INF/cgi/test.pl"};
но если я добавлю следующее перед .waitFor ():
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ( (line = br.readLine()) != null){
System.out.println(line);
}
Я получаю отпечаток:
Can't open perl script "/WEB-INF/cgi/test.pl": No such file or directory
Я думаю, это возвращение к исходной проблеме?