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!

2
  • 1
    support.microsoft.com/kb/110930 Commented Nov 4, 2012 at 1:59
  • 1
    "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. Commented Nov 4, 2012 at 2:02

3 Answers 3

33

Use:

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

The 2 redirects the error stream.

Sign up to request clarification or add additional context in comments.

3 Comments

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!
This answer was very useful. In testing however, I found the "2" outputs EVERYTHING, not just errors.
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?
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

1 Comment

I tested this. For some reason this doesnt output/log ANY of the java.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.