penglu
asked on
problem of capturing process ouput in servlet
Hello,
I want to capture Fortran process output inside java servlets and display them on the web. But I got error message like
"System Error: Bad File descriptor, CLOSE(Unit=7....)". And no further output displayed after some. I execute the Fortran process through shell script. The java servlet and Fortran process are all located in a Linux box.
I have no problem to run it under console based stand alone application.
/* make a shell script*/
BufferedWriter bw2=new BufferedWriter(new FileWriter(currentDir+"/pe ntran.scri pt"));
bw2.write("#!/bin/sh"+ "\n");
bw2.write("cd "+currentDir+"/"+"\n");
bw2.write("echo sample.f90 "+" | "+" /opt/tomcat/pentran/pentra n" );
bw2.close();
/*create a process*/
Runtime rt = Runtime.getRuntime();
/*chmod permission*/
Process changeMode = rt.exec( "chmod 755 " + currentDir + "/pentran.script" );
Process pr = rt.exec(currentDir+"/pentr an.script" );
/*capture output */
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getIn putStream( )));
String str = br.readLine();
while(str != null )
{
pr.println(str+ "\n" );
str=br.readLine();
}
Does anyone has any ideas why?
Best Regards,
Peng
I want to capture Fortran process output inside java servlets and display them on the web. But I got error message like
"System Error: Bad File descriptor, CLOSE(Unit=7....)". And no further output displayed after some. I execute the Fortran process through shell script. The java servlet and Fortran process are all located in a Linux box.
I have no problem to run it under console based stand alone application.
/* make a shell script*/
BufferedWriter bw2=new BufferedWriter(new FileWriter(currentDir+"/pe
bw2.write("#!/bin/sh"+ "\n");
bw2.write("cd "+currentDir+"/"+"\n");
bw2.write("echo sample.f90 "+" | "+" /opt/tomcat/pentran/pentra
bw2.close();
/*create a process*/
Runtime rt = Runtime.getRuntime();
/*chmod permission*/
Process changeMode = rt.exec( "chmod 755 " + currentDir + "/pentran.script" );
Process pr = rt.exec(currentDir+"/pentr
/*capture output */
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getIn
String str = br.readLine();
while(str != null )
{
pr.println(str+ "\n" );
str=br.readLine();
}
Does anyone has any ideas why?
Best Regards,
Peng
ASKER
Hi Mayankeagle,
Thanks for the suggestions. After I tried, I got the same result. Any more suggestions?
Also, I saw the following error message from catalina.log file:
Apr 15, 2004 9:17:30 AM org.apache.commons.digeste
SEVERE: Parse Error at line 79 column 15: The content of element type "servlet" is incomplete, it must match "(icon?,servlet-name,displ
org.xml.sax.SAXParseExcept
at org.apache.xerces.util.Err
at org.apache.xerces.util.Err
at org.apache.xerces.impl.XML
at org.apache.xerces.impl.XML
at org.apache.xerces.impl.dtd
at org.apache.xerces.impl.dtd
at org.apache.xerces.impl.XML
at org.apache.xerces.impl.XML
at org.apache.xerces.impl.XML
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at org.apache.commons.digeste
at org.apache.catalina.startu
at org.apache.catalina.startu
at org.apache.catalina.startu
at org.apache.catalina.util.L
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.startu
at org.apache.catalina.startu
at org.apache.catalina.startu
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at org.apache.catalina.startu
Apr 15, 2004 9:17:31 AM org.apache.coyote.http11.H
INFO: Starting Coyote HTTP/1.1 on port 8080
Apr 15, 2004 9:17:31 AM org.apache.jk.common.Chann
INFO: JK2: ajp13 listening on /0.0.0.0:8009
Apr 15, 2004 9:17:32 AM org.apache.jk.server.JkMai
INFO: Jk running ID=0 time=0/78 config=/opt/tomcat/conf/jk
Peng
Does the changeMode execute fine? Have you checked which line in your code throws the exception?
ASKER
Yes. It was executed fine. The error message comes from pr1.getErrorStream(). I tried to print any exception message, but nothing.
Are you reading this in a Servlet? There shouldn't be any problems but maybe that the servlet needs to overcome some security to read it.
ASKER
Yes. It is reading in the servlet. It can read part of the output, but stop in the middle saying "system error , bad file descriptor,..." while it is running perfectly fine in a dummy stand alone application ( with main() function).
Any more thoughts?
Ah, so its reading part of the output properly. That is significant. Then I guess that the stream perhaps gets corrupted or something. Also, make sure that you're not running another instance of the process. Try using a DataInputStream () instead of a BufferedReader and see if that helps.
ASKER
I tried DataInputStream, still no luck.
Can you post the DataInputStream code? Does it give the same error (reads output till some extent and then gives error)?
ASKER
sorry. I just deleted it. It did the same thing. No difference with using BufferedReader.
ASKER
I tried "ls", it is working. I am able to get all the ouput. Seems it is not working for other non system process. Any other thought?
ASKER
Now the problem seems that Fortran process is writting an output while we get input at the same time. These two conflict each other. However it is running fine under Windows platform.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Runtime rt = Runtime.getRuntime();
Process changeMode = rt.exec( "chmod 755 " + currentDir + "/pentran.script" );
rt = Runtime.getRuntime();
Process pr = rt.exec(currentDir+"/pentr