wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes problem reading output of a subprocess Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem reading output of a subprocess" Watch "problem reading output of a subprocess" New topic
Author

problem reading output of a subprocess

abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
Hi,

I am running a Perl script in my Java code.The problem is I am not able to capture the output of the Perl script and then write it into a file.Here is that part of my code which deals with running the Perl script, capturing the output and writing it to a file.

------------------------------------------------------------------------
<code>

public void itemStateChanged(java.awt.event.ItemEvent e) {
if (e.getStateChange() == e.SELECTED) {
System.out.println(e.getItem());
File f = new File("scripts\\"+e.getItem());
if (! f.exists())
{
JOptionPane.showMessageDialog(null,e.getItem()+" does not exists","LAB DIALER ERROR", 1);
}
String cmd = "perl 'D:\\JAVA\\Eclipse3.2\\Workspace\\Abhinav\\scripts\\' +e.getItem()";
String logdir = null;
int c;
BufferedInputStream stdout = null;
BufferedInputStream stderr = null;
Process p = null;
try {
p = Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
stdout = new BufferedInputStream(p.getInputStream());
stderr = new BufferedInputStream(p.getErrorStream());
File fLogdir = new File(logdir);
fLogdir.mkdir();
FileWriter stderrWriter = null;
try {
stderrWriter = new FileWriter(new File(logdir+p+"mantis.err"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while( (c = stderr.read()) != -1)
{
stderrWriter.write(c);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stderrWriter.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stderrWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

// capture stdout
FileWriter stdoutWriter = null;
try {
stdoutWriter = new FileWriter(new File(logdir+p+"mantis.out"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while( (c = stdout.read()) != -1)
{
stdoutWriter.write(c);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stdoutWriter.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stdoutWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}


// TODO Auto-generated Event stub itemStateChanged()
}
});

}
return jComboBox;
}

<\code>

-----------------------------------------------------------------------


Please tell me where the problem is and what is causing it.I want to know the cause.If anybody needs,I can also give the full code.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

Have a look at this article, which explains the problems with Runtime.exec and how to work around them.


[How To Ask Questions On JavaRanch]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

I strongly advise you not to mix InputStream/OutputStream with Reader/Writer; one treats integers as bytes, whereas the other treats integers as characters.

If I were you, I'd either switch to a FileOutputStream, or use BufferedReaders combined with InputStreamReader.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem reading output of a subprocess