Solved

problem of capturing process ouput in servlet

Posted on 2004-04-14
15
542 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:mayankeagle
Comment Utility
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
Comment Utility

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:mayankeagle
Comment Utility
Does the changeMode execute fine? Have you checked which line in your code throws the exception?
0
 

Author Comment

by:penglu
Comment Utility

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:mayankeagle
Comment Utility
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
Comment Utility

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility

I tried DataInputStream, still no luck.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility

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

0
 

Author Comment

by:penglu
Comment Utility

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
Comment Utility


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:
mayankeagle earned 500 total points
Comment Utility
>> 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
pairstar challenge 2 41
wordmultiple challenge 12 89
recursion example 16 66
Not needed 13 53
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

771 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

10 Experts available now in Live!

Get 1:1 Help Now