Home > Mobile >  How to Parse Java Command Output in Windows Command Line
How to Parse Java Command Output in Windows Command Line

Time:12-16

My goal is to be able to parse the output of the following windows command line command (and in powershell too) :

java -XshowSettings:properties -version

I am trying to specifically get the line that says "java.home = Path\to\java\home"

So far I have tried commands like

java -XshowSettings:properties -version | findstr /C:java.home

or redirecting to a file and parsing from there but the file is empty

 java -XshowSettings:properties -version > tmp.txt

I also tried the following powershell commands

java -XshowSettings:properties -version | Select-String -Pattern 'java.home' -SimpleMatch
java -XshowSettings:properties -version | Out-String -Stream | Select-String -Pattern 'home' -SimpleMatch

I even tried making a function for the command and setting a variable to the command output which I would then try to parse, but the variable was also empty

function javahome {
   java -XshowSettings:properties -version
}
$Jhome = javahome

$Jhome is empty...

I think that the output of the java command is not outputting through the same pipes that the command lines are capturing even though it is being output on the screen. I'm not sure how to find this separate output and begin parsing it, but that is what I need to do. I need to be able to parse out the java.home path as it is needed for another process. I also will need other lines too, so I am not looking to pull the %JAVA_HOME% environment variable.

CodePudding user response:

This command writes to standard error, not to standard output.

For command line, try
java -XshowSettings:properties -version 2>&1 | findstr /C:java.home
The 2>&1 redirects standard error to standard output.

Or java -XshowSettings:properties -version 2> tmp.txt
redirecting standard error to file.

  • Related