Solved

problem of capturing process ouput in servlet

Posted on 2004-04-14
15
546 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:penglu
  • 7
  • 6
15 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10832203
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");

0
 

Author Comment

by:penglu
ID: 10832895

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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10832950
Does the changeMode execute fine? Have you checked which line in your code throws the exception?
0
 

Author Comment

by:penglu
ID: 10833201

Yes. It was executed fine. The error message comes from pr1.getErrorStream(). I tried to print any exception message, but nothing.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10833285
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.
0
 

Author Comment

by:penglu
ID: 10833394

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?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10833483
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.
0
 

Author Comment

by:penglu
ID: 10833858

I tried DataInputStream, still no luck.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10833980
Can you post the DataInputStream code? Does it give the same error (reads output till some extent and then gives error)?
0
 

Author Comment

by:penglu
ID: 10834478

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

0
 

Author Comment

by:penglu
ID: 10835398

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?

 
0
 

Author Comment

by:penglu
ID: 10838031


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.


0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 10839343
>> Now the problem seems that Fortran process is writting an output while we get input at the same time. These two conflict each other

That is supposed to be the whole idea. We are supposed to read from what the Fortran process writes. And that is the way to read from it. So there should not be any conflicting.

>> However it is running fine under Windows platform

Maybe that you have a different version of JDK in Windows. Or maybe that the Fortran process in UNIX was also conflicting with some other UNIX process.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now