Calling external programs from within a Tomcat application

P

pwaring

I've got a fairly complex existing Tomcat application (which is
packaged and built as a .war file) which I'm trying to edit so that it
calls a Perl script part way through the processing, which will
generate an XML file used later on. I think I've worked out how to
call external scripts from within Java, but at the moment when I try
and access the application via Tomcat the application either hangs or
bails out (I don't know which, as the log files unhelpfully don't give
any error messages).

The code which is causing the problem looks like this:

System.err.println("Calling runtime...");
Runtime runtime = Runtime.getRuntime();
System.err.println("Executing process...");
Process process = runtime.exec("/path/to/ysearch.pl 'News' '\"search
query\"' 'file");
System.err.println("Waiting for process...");
int exitVal = process.waitFor();
System.out.println("Exited with error code: " + exitVal);

The code gets as far as "Executing process..", beyond that there is
nothing in the log file so I presume the runtime.exec() call is where
the problem is. I'm not interested in reading the output from the
script (there shouldn't be any), so that's not an issue, and the
permissions on it allow anyone to read or execute ysearch.pl so I
don't think there's a problem in that area.

Does anyone have any suggestions which I could try to get this to
work? I've only been using Tomcat for a week (I'm picking up on
someone else's code) so I might have made a beginner's mistake. I'm
using Tomcat 5 on Fedora Core 7, and Java 1.5.0_01 (I can't easily
change any of those).
 
S

shakah

I've got a fairly complex existing Tomcat application (which is
packaged and built as a .war file) which I'm trying to edit so that it
calls a Perl script part way through the processing, which will
generate an XML file used later on. I think I've worked out how to
call external scripts from within Java, but at the moment when I try
and access the application via Tomcat the application either hangs or
bails out (I don't know which, as the log files unhelpfully don't give
any error messages).

The code which is causing the problem looks like this:

System.err.println("Calling runtime...");
Runtime runtime = Runtime.getRuntime();
System.err.println("Executing process...");
Process process = runtime.exec("/path/to/ysearch.pl 'News' '\"search
query\"' 'file");
System.err.println("Waiting for process...");
int exitVal = process.waitFor();
System.out.println("Exited with error code: " + exitVal);
[...rest of post snipped...]

I usually the String-array form of Runtime.exec() to avoid shell
quoting problems (though that doesn't appear to be your problem here).
Beyond that, try running the command from a shell prompt while logged
in as the user Tomcat runs under, maybe you'll find you have a
LD_LIBRARY_PATH problem or something. FWIW, the code which uses the
String-array form of Runtime.exec() ends up something like:

String [] asCmd = new String[4] ;
asCmd[0] = "/path/to/ysearch.pl" ;
asCmd[1] = "News" ;
asCmd[2] = "search query" ;
asCmd[3] = "filename" ;

java.lang.Process p = java.lang.Runtime.getRuntime().exec(asCmd) ;
p.waitFor() ;

java.io.InputStream is = null ;
try {
is = p.getInputStream() ;
java.io.BufferedInputStream bis = new
java.io.BufferedInputStream(is) ;

byte [] ab = new byte[64000] ;
int nTotalBytesRead = 0 ;
while(nTotalBytesRead < 64000) {
int nBytesRead = bis.read(ab, nTotalBytesRead, 64000 -
nTotalBytesRead) ;
if(nBytesRead < 0) {
break ;
}
else {
nTotalBytesRead += nBytesRead ;
}
}

return new String(ab, 0, nTotalBytesRead) ;
}
finally {
if(null != is) {
try {
is.close() ;
}
catch(Exception x) {
}
is = null ;
}
}
 
P

pwaring

I usually the String-array form of Runtime.exec() to avoid shell
quoting problems (though that doesn't appear to be your problem here).

I didn't think that would be the problem either, but after using the
method you suggested it worked perfectly (though I had to change some
permissions beforehand) - thanks!

Paul
 
T

tmpuser

I didn't think that would be the problem either, but after using the
method you suggested it worked perfectly (though I had to change some
permissions beforehand) - thanks!

Paul

I'm having a similar problem. Can you please tell me what permissions
did you set up?
thanks
 
P

pwaring

I'm having a similar problem. Can you please tell me what permissions
did you set up?

I just created a directory called /tomcat/ with the following
permissions:

drwxr-xr-x 2 tomcat tomcat 12288 2008-07-28 09:49 tomcat

Ugly hack I know, but I just needed a quick solution that worked.

Paul
 
G

gimme_this_gimme_that

Have you considered running a modified version of the Perl script as a
CGI?

My recollection is that Tomcat CGI is turned off by default - but it
can be configured to be turned on.
 
L

Lew

No, because I don't know how to do that, either from the Perl side (I
only use Perl scripts on the command line) or via Tomcat (which I'm
only using for this because of someone else's code - I would never
touch it out of choice).

Project is a proven sling and generally unsuitable to harass as bushes go. I
wonder what your lighter with it is.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Degrees do not matter...one does not bargain about inches of evil."

--- Ayn Rand, Atlas Shrugged
 
P

pwaring

Have you considered running a modified version of the Perl script as a
CGI?

My recollection is that Tomcat CGI is turned off by default - but it
can be configured to be turned on.

No, because I don't know how to do that, either from the Perl side (I
only use Perl scripts on the command line) or via Tomcat (which I'm
only using for this because of someone else's code - I would never
touch it out of choice).

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top