Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command line "java -version" will send result to stdOut or stdErr? [duplicate]

Tags:

java

winapi

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?

like image 481
milesma Avatar asked Oct 31 '22 13:10

milesma


1 Answers

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.

like image 129
Elliott Frisch Avatar answered Nov 13 '22 06:11

Elliott Frisch