简体   繁体   中英

Simple Java Process Input/Output failing

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?

The issue you have here is almost certainly that the "java Memory" command is failing.

When you run a process, Java creates three outputs, the exit code, the STDOUT, and the STDERR. A good program running an external process, will check all three.

You are just checking the STDOUT.

If you change the code after you build the proc, to be:

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

you will get a better idea of what went wrong.

It is hard to do, managing those three outputs, and many systems implement a separate thread to control the STDERR and STDOUT streams. How you solve it though is beyond the scope of this quesiton.

I tried your code as written and it ran just fine...

Are you trying to run it from the command line or from within an IDE? I tried running it from within Eclipse and received an IO error. It wasn't registering where Memory.class was.

I exited to the file system and navigated to where Memory.class and Processor.class were. Executing from within that directory worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM