5

I'm writing code to use Win32 API to detect a java's version. E.g.

Basically, I'm following MSDN Creating a Child Process with Redirected Input and Output https://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx

string GetJavaVersion(string sJavaExePath) {
}

This is the pseudo client code:

ASSERT(GetJavaVersion("C:\Program Files (x86)\Java\jdk1.7.0_17\bin\java.exe") == "1.7.0_25");

I can get the result as:

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)

However, the result is sent back from stdErr, while I though it should return from stdOut.

Does it make sense to receive the string from stdErr?

milesma
  • 308
  • 3
  • 11
  • 1
    Why? Where does it say it goes to stdout? What is the basis for your expectation? – user207421 Dec 15 '15 at 02:52
  • I don't know the conversion of using stdout or stderr by Java. My basis is during the run, there "seems" no error happened, so it is very intuitive to expect it to go to stdout. – milesma Dec 15 '15 at 03:04

1 Answers1

2

The answer is stderr. We can redirect stderr and stdout seperately and see,

$ java -version 2>stderr.txt 1>stdout.txt
$ cat stderr.txt 
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

You can also redirect stderr to stdout,

$ java -version 2>&1

which would allow you to read it from stdout.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249