Link to home
Start Free TrialLog in
Avatar of penglu
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+"/pentran.script"));
                bw2.write("#!/bin/sh"+ "\n");
                bw2.write("cd "+currentDir+"/"+"\n");
                bw2.write("echo sample.f90 "+" | "+" /opt/tomcat/pentran/pentran" );
                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+"/pentran.script");

                /*capture output */
                BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                String str = br.readLine();
                while(str != null )
                {
                    pr.println(str+ "\n" );
                    str=br.readLine();
                }

Does anyone has any ideas why?

Best Regards,
Peng
Avatar of Mayank S
Mayank S
Flag of India image

The code seems to be correct. Try making the Run-time object again:

Runtime rt = Runtime.getRuntime();          
Process changeMode = rt.exec( "chmod 755 " + currentDir + "/pentran.script" );
rt = Runtime.getRuntime();          
Process pr = rt.exec(currentDir+"/pentran.script");

Avatar of penglu
penglu

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.digester.Digester error
SEVERE: Parse Error at line 79 column 15: The content of element type "servlet" is incomplete, it must match "(icon?,servlet-name,display-name?,description?,(servlet-class|jsp-file),init-param*,load-on-startup?,run-as?,security-role-ref*)".
org.xml.sax.SAXParseException: The content of element type "servlet" is incomplete, it must match "(icon?,servlet-name,display-name?,description?,(servlet-class|jsp-file),init-param*,load-on-startup?,run-as?,security-role-ref*)".
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
        at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
        at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.commons.digester.Digester.parse(Digester.java:1543)
        at org.apache.catalina.startup.ContextConfig.applicationConfig(ContextConfig.java:282)
        at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:639)
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:243)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:166)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:3567)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1188)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:738)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1188)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:347)
        at org.apache.catalina.core.StandardService.start(StandardService.java:497)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:2190)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:512)
        at org.apache.catalina.startup.Catalina.execute(Catalina.java:400)
        at org.apache.catalina.startup.Catalina.process(Catalina.java:180)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203)
Apr 15, 2004 9:17:31 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on port 8080
Apr 15, 2004 9:17:31 AM org.apache.jk.common.ChannelSocket init
INFO: JK2: ajp13 listening on /0.0.0.0:8009
Apr 15, 2004 9:17:32 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/78  config=/opt/tomcat/conf/jk2.properti


Peng
Does the changeMode execute fine? Have you checked which line in your code throws the exception?
Avatar of penglu

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.
Avatar of penglu

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.
Avatar of penglu

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)?
Avatar of penglu

ASKER


sorry. I just deleted it. It did the same thing. No difference with using BufferedReader.

Avatar of penglu

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?

 
Avatar of penglu

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
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial