• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

problem reading output of a subprocess

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this article, which explains the problems with Runtime.exec and how to work around them.
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic