How to read the std output of another java program in this java program?

后端 未结 2 1008
小蘑菇
小蘑菇 2021-01-17 01:40

I write a simple Java program, which just output some \"hello\"s to std every 5s.

public class DDD {
    public static void main(String[] args) throws Interr         


        
相关标签:
2条回答
  • 2021-01-17 02:39

    BufferedReader#readLine will return null when it reaches the end of the stream.

    Because you're basically ignoring this exit indicator and looping in an infinite loop, all you get is null.

    The likely cause is because the process has output some error information to the error stream, which you are not reading...

    You should try using ProcessBuilder instead, which allows you to, amongst other things, redirect the error stream into the input stream

    try {
        String[] command = {"java.exe", "mytest.DDD"};
        ProcessBuilder pb = new ProcessBuilder(command);
        // Use this if the place you are running from (start context)
        // is not the same location as the top level of your classes
        //pb.directory(new File("\path\to\your\classes"));
        pb.redirectErrorStream(true);
        Process exec = pb.start();
    
        BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        String text = null;
        while ((text = br.readLine()) != null) {
            System.out.println(text);
        }
    } catch (IOException exp) {
        exp.printStackTrace();
    }
    

    ps- This will work if java.exe is your path, otherwise you will need to provide the full path to the executable as you already have done in your example ;)

    0 讨论(0)
  • 2021-01-17 02:43

    First, your program is totally correct. By that I mean the way you launch the process and read the input stream should work. So let's see why it doesn't.

    I ran your program and I encountered the same behavior. To understand why it didn't work, I made a simple change: instead of reading getInputStream(), I listened to getErrorStream(). This way, I could see if the java command returned an error instead of launching the program. And sure enough, it printed the following message:

    Error: Could not find or load main class myTest.DDD
    

    That's it, and I guess it's probably the case for you too. The program could simply not find the DDD class because it's not in the classpath.

    I work in Eclipse and the classes are compiled into the directory bin in the project, so I simply changed the command to

    String command = "c:\\java\\jdk1.7.0_07\\bin\\java -cp bin mytest.DDD";
    

    and it worked. I obtained (after switching back to getInputStream()):

    hello 0
    hello 1
    hello 2
    hello 3
    

    This means that by default the working directory for processes spawned by the command exec is the root of the project, not the directory where the classes are compiled.

    In conclusion, just specify the classpath and it should work fine. If not, look at what the error stream contains.

    Note: You could have guessed the reason: the Javadoc specifies that readline() returns null if the end of the stream has been reached. It was a clear indicator that the process was terminated early.

    0 讨论(0)
提交回复
热议问题