5

I am trying the following in my cmd prompt:

java>temp.txt

temp.txt is getting created with no content in it. "java" command output is displayed in console itself without getting redirecting to temp.txt

But if i do the following:

dir> temp.txt

output of dir command is getting redirected to temp file.

Why is the output of "java" command not getting redirected to the text file??

2 Answers 2

12

The > is for sending the standard output (stdout) to a given location. But the output printed with java command is coming in standard error (stderr) output.

To redirect stderr, you can use the below syntax. This will redirect standard outputs to stdout.txt and standard errors to stderr.txt

java > stdout.txt 2> stderr.txt

Alternatively you can send both standard output and standard error to same file using below command (without mentioning the filenames twice).

java > output.txt 2>&1

2> means redirect the stderr, &1 means to where the stdout is redirected which is output.txt. So this command will redirect stdout to output.txt and stderr to where the stdout is redirected (which is output.txt iteself).

You can also ignore the stderr by sending it to say, /dev/null (only linux)

java > out.txt 2> /dev/null
Sign up to request clarification or add additional context in comments.

1 Comment

what if: java 2>&1
1

Because when you run java without arguments, the output is redirected to stderr instead of stdout. So you should do:

java 2>temp.txt

2 tells the command to redirect stderr to the file temp.txt

More details on http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

2 Comments

Thanks! That worked. But how do you know that "java" command redirects output to stderr.
I didn't know, but I've made a test redirecting stdout and stderr to a file, and found out the answer

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.