I write a simple Java program, which just output some \"hello\"s to std every 5s.
public class DDD {
public static void main(String[] args) throws Interr
BufferedReader#readLine
will return null
when it reaches the end of the stream.
Because you're basically ignoring this exit indicator and looping in an infinite loop, all you get is null
.
The likely cause is because the process has output some error information to the error stream, which you are not reading...
You should try using ProcessBuilder
instead, which allows you to, amongst other things, redirect the error stream into the input stream
try {
String[] command = {"java.exe", "mytest.DDD"};
ProcessBuilder pb = new ProcessBuilder(command);
// Use this if the place you are running from (start context)
// is not the same location as the top level of your classes
//pb.directory(new File("\path\to\your\classes"));
pb.redirectErrorStream(true);
Process exec = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String text = null;
while ((text = br.readLine()) != null) {
System.out.println(text);
}
} catch (IOException exp) {
exp.printStackTrace();
}
ps- This will work if java.exe
is your path, otherwise you will need to provide the full path to the executable as you already have done in your example ;)