I have addressed many issues regarding input / output from a spawned process in Java, but I still cannot figure out what is wrong with my program. It is very short and simple, but it does not work. I use the exec () method of the Runtime object to create a new process (I know that ProcessBuilder is probably better, but it's for a simple assignment, and I really don't need to worry about the stderr of the generated process).
I run the "Processor" class myself from the IDE / command prompt. Memory.java is in the same directory.
public class Memory
{
public static void main(String[] args)
{
System.out.println("Memory works!");
}
}
And another program:
import java.io.IOException;
import java.io.InputStream;
public class Processor
{
public static void main(String[] args)
{
Runtime rt = Runtime.getRuntime();
Process proc = null;
try {
proc = rt.exec("java Memory");
} catch (IOException e) {
e.printStackTrace();
System.exit(2);
}
InputStream fromProcess = proc.getInputStream();
int x;
try {
while((x = fromProcess.read()) != -1)
System.out.print((char)x);
} catch (IOException e) {
e.printStackTrace();
}
}
}
There is no way out and the program never ends. Now this may be due to a while loop in memory for the scanner, but then there must be some way out, at least correctly?