Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem reading output of a subprocess

 
abhinav sinha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this article, which explains the problems with Runtime.exec and how to work around them.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic