1

I have to send the java version to a text file as below

C:\>java -version > C:\cfn\log\JavaVersion.log

But when i open the check the file, its completely Empty. I get the version immediately at the below line as-

C:\>java -version > C:\cfn\log\JavaVersion.log
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

How do i fix this?

Jaqueline Vanek
  • 1,100
  • 10
  • 20
Alan Jebakumar
  • 167
  • 2
  • 12
  • Look at the dup and the answers. They like to others as well. java is sending this information to stderr. This is a common practive. PowerShell sees it as a fault. – Matt Feb 17 '17 at 19:01

2 Answers2

0

try this it will create a file on the current path.

java -version > JavaVersion.log 2>&1

or this one, which will create file on the specified path

java -version > C:\cfn\log\JavaVersion.log 2>&1

The syntax 2>&1 will redirect 2 (stderr) to 1 (stdout). You can also hide messages by redirecting to NUL

Inus Saha
  • 1,918
  • 11
  • 17
  • improved my answer, check it again. – Inus Saha Feb 17 '17 at 19:02
  • This was exactly what I was looking out for.. You made my day. Thanks much :) – Alan Jebakumar Feb 19 '17 at 08:46
  • When I try the same thing in powershell as `& java -version > C:\JavaVersion.log 2>&1`, & when I try opening the file's content, the below is what I see- `java.exe : java version "1.8.0_121" At line:1 char:1 + & java -version > C:\JavaVersion.log 2>&1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (java version "1.8.0_121":String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Java(TM) SE Runtime Environment (build 1.8.0_121-b13 ) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)`. – Alan Jebakumar Feb 19 '17 at 08:58
  • This fixed the above `& cmd /c "java -version > C:\javaVersion.log 2>&1"` – Alan Jebakumar Feb 19 '17 at 09:05
0

On my mac,

java -version

prints to stderr.

To redirect stderr to a file, do something like:

java -version 2> foo.txt

For how to do this on Windows, see:

How to capture stderr on Windows/DOS?

Community
  • 1
  • 1
aloraine
  • 106
  • 1
  • 5