Calling perl script from Java servlet -
i'm trying call perl script java servlet on tomcat 7. have set context.xml , web.xml can run .pl file going http://localhost:8080/test/cgi-bin/test.pl , can run perl directly within java so:
string[] cmdarr = new string[]{"perl", "-e", "print \"content-type: text/html\n\n\";$now = localtime();print \"<h1>it $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 works fine, if try , refer .pl script in /web-inf/cgi folder replacing the:
string[] cmdarr = new string[]{"perl", "-e", "print \"content-type: text/html\n\n\";$now = localtime();print \"<h1>it $now</h1>\";"};
with like:
string cmdarr = "/web-inf/cgi/test.pl";
or
string cmdarr = "/cgi-bin/test.pl";
i keep getting 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 file path somewhere? appreciated!
update: after @hobbs comment changed to: string[] cmdarr = new string[]{"perl", "/web-inf/cgi/test.pl"};
but if add following before .waitfor():
bufferedreader br = new bufferedreader(new inputstreamreader(p.geterrorstream())); string line; while ( (line = br.readline()) != null){ system.out.println(line); }
i print:
can't open perl script "/web-inf/cgi/test.pl": no such file or directory
i guess original problem?
somewhat confusingly, when exec syscall returns "no such file or directory" on script, means interpreter on shebang line couldn't found. e.g. if /web-inf/cgi-test.pl starts #!/usr/bin/perl
/usr/bin/perl doesn't exist. or if file has windows line-endings, lead kernel looking interpreter named "/usr/bin/perl\x0a"
, can't found.
since you've established can run things using command array starts "perl"
, how about:
string cmdarr = new string[]{"perl", "/web-inf/cgi/test.pl"};
?
Comments
Post a Comment