4

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?

3
  • "I don't really have to worry about the stderr of the spawned process" hum... take a look at it : it will certainly tell you going wrong ;-) Commented Oct 4, 2014 at 15:22
  • I d recommend you a couple of debugging steps you could use to understand what's happening. First, do care for stderr as it could be pointing you the problem. Second, try running some other command, something you re sure it will output to stdout as the ls or dir command depending on which os you re working, If you see output this could also tell you which current directory is being used which would be helpful just to be sure we re in the right context. Lastly i just for checking, does the current directory of the processors class execution has the compiled memory.class file in it? Commented Oct 4, 2014 at 15:30
  • I have rolled back your massive edits... as they make the current answers useless. If you still are having problems with your code, and the other advice listed like adding debugging steps, then ask a separate question that narrows down the new problems you have, and include the relevant output Commented Oct 4, 2014 at 15:51

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have edited the program to where now Processor sends data to stdin of memory and memory prints it out to stdout (which is sent to Processor's fromStream). But nothing happens. I would really appreciate your help again, thanks!
1

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.

2 Comments

Yes, that was the error. I was running it from Eclipse and didn't check the error stream. But now when I try to send data back and forth I have problems. Please see edited question. Thanks again!
Did you try running it from the command line? Like I said, it runs just fine, as written, for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.