Will the command line "java -version" send the result to stdOut or stdErr?

I am writing code to use the Win32 API to determine the version of Java. For instance.

Basically, I follow MSDN Creating a child process with redirected inputs and outputs 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, although I have to return from stdOut.

Does it make sense to get a string from stdErr?

+5
source share
1 answer

The answer is stderr. We can redirect stderr and stdout separately 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 will let you read it from stdout.

+2
source

Source: https://habr.com/ru/post/1238210/


All Articles