JAVA process hang when expectts error stream from C++ application
Hi friends,
My JAVA program has to call c++ (kkk) application and JAVA opens 2
threads to read outputstream and errorstream from c++ application.
See the code below). Since errorstream is expecting some data it hangs
unless c++ feeds the same.
When I used alternative 'perror("")'/'perror(NULL)' in kkk.c hang gets
solved but c++ standalone program keeps throwing dummy error on
screen.
Could anybody help me here how it can be rectified.
Thanks in advance
- Krish
------------
start.java:
-----------
public class start
{
public static void main(String [] args) throws
IOException,InterruptedException
{
String cmd = "/home/user/kkk";
Process process = Runtime.getRuntime().exec(cmd);
MyReader outReader = new MyReader(process.getInputStream());
Thread outThread = new Thread(outReader);
outThread.start();
MyReader errReader = new MyReader(process.getErrorStream());
Thread errThread = new Thread(errReader);
errThread.start();
System.out.println("exit code is " + process.waitFor());
//process.destroy();
// process.getInputStream().close();
// process.getErrorStream().close();
outThread.join();
errThread.join();
System.out.println("joins completed");
}
}
/***** MyReader.java *******/
import java.io.*;
public class MyReader implements Runnable
{
private final InputStream is;
public MyReader(InputStream is)
{
this.is = is;
}
public void run()
{
// normally would read in while here, but these threads never get
any
// they are blocked on the first read() call forever.
int length;
try
{
length = is.read();
if (length == -1)
{
System.out.println("-1");
return;
}
if (length == 0)
{
System.out.println("0");
return;
}
System.out.println("read something");
} catch (Throwable t) {
System.out.println(t);
}
}
}
kkk.c:
-------
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
# include <sys/wait.h>
int main()
{
perror ("");
// perror (NULL);
return 0;
}