Simple Java Process I / O

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");  //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 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?

+4
2

, , , "java Memory" .

, Java , , STDOUT STDERR. , , .

STDOUT.

proc, :

    //stream to get output from memory
    InputStream fromProcess = proc.getInputStream();
    InputStream fromError = proc.getErrorStream();

    int x;

    try {
        while((x = fromProcess.read()) != -1)
            System.out.print((char)x);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        while((x = fromError.read()) != -1)
            System.err.print((char)x);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int exit = proc.exitValue();
    System.out.println("Exited with code " + exit);

, .

, , STDERR STDOUT. , quesiton.

+3

, , ...

IDE? Eclipse -. , Memory.class.

, Memory.class Processor.class. .

0

All Articles