I have looked at many questions regarding input/output from spawned process in Java but I still can't figure out what is wrong with my program. It's really short and simple but it doesn't work. I am using the exec() method of the Runtime object to create the new process (I know that ProcessBuilder is probably better, but this is for a simple assignment and I don't really have to worry about the stderr of the spawned process).
I am running the "Processor" class myself from the IDE/command line. Memory.java is in the same directory.
public class Memory
{
public static void main(String[] args)
{
System.out.println("Memory works!");
}
}
And the other 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"); //execute Memory.java which is in same directory
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(2); //exit code 2 means that process creation failed.
}
//stream to get output from memory
InputStream fromProcess = proc.getInputStream();
int x;
try {
while((x = fromProcess.read()) != -1)
System.out.print((char)x);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
There is no output and the program never terminates. Now that could be because of the while loop in Memory for the Scanner, but then there should be some output at least right?