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

后端 未结 2 1047
小蘑菇
小蘑菇 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条回答
  •  猫巷女王i
    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.

提交回复
热议问题