Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem of capturing process ouput in servlet

Posted on 2004-04-14
15
Medium Priority
?
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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
 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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 …
Introduction to Processes

721 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