Simple Java Process Input/Output failing

拜拜、爱过 提交于 2019-12-13 13:28:52

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/26194144/simple-java-process-input-output-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!