How to output java -jar errors to a text/log file?
Asked Answered
G

3

14

i have googled my heart out! I am trying to figure out how to output any errors a java class might give when executing java from the Windows command line.

For instance

java -jar class.jar <someFile.file>

if that line throws any errors, i want them to be stored into a text file for later reviewing.

I tried

java -jar class.jar <someFile.file> >> log.txt

But despite throwing errors the log.txt file is empty.

Thanks all!

Gestate answered 4/11, 2012 at 1:57 Comment(2)
support.microsoft.com/kb/110930Seabury
"i have googled my heart out!" Better to mention 1) What search terms were used. 2) Some of the top links found on those terms. 3) Why they did not suit the need.Compressive
K
34

Use:

java -jar class.jar <someFile.file> 2>> log.txt

The 2 redirects the error stream.

Keith answered 4/11, 2012 at 2:6 Comment(3)
I thank you greatly! Never knew that after all the batch files i wrote :( And the search terms i used were "Output java -jar errors" and they didnt hold my answer. Nothing even close. Would have never thought to use redirect error stream. Thank you so much @thedayofcondor!Gestate
This answer was very useful. In testing however, I found the "2" outputs EVERYTHING, not just errors.Drucie
The 2>> only logs stderr to the file, stdout should be visible on the console. It may be your jar prints its output as well to stderr?Keith
E
1

or you can use bat file to easily direct all output to a text file in command prompt

C:> something.bat > console_output.txt

Etui answered 4/12, 2013 at 7:28 Comment(1)
I tested this. For some reason this doesnt output/log ANY of the java.Drucie
M
0

Try to do it like this to running it background

javaw -jar yourjarfile.jar >>yourlogfile.txt 2>&1

Try the one below if you want to run it on cmd session.

java -jar yourjarfile.jar >>yourlogfile.txt 2>&1
Mccully answered 24/9, 2023 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.