-1

I am running java -jar xyz.jar command on the linux terminal. I have both system.out.println statements and System.exit statements in the code.

how to capture the system.exit or output to the OS on the linux? do i need to prepare a linux script for this?

2
  • just redirect jar whatever ... > redirect.txt Commented Sep 27, 2016 at 17:12
  • 1
    What do you mean by "to the OS"? Do you want to write it to a file so that other program/process can use it? Commented Sep 27, 2016 at 17:12

2 Answers 2

5

This may be off-topic for SO.

You just redirect it:

java -jar xyz.jar > the_file_to_output

If you used System.err output as well as System.out output and wanted to redirect both, it's a tiny bit more complicated because you need to redirect out to the file and then redirect error to out:

java -jar xyz.jar > the_file_to_output 2>&1

The 2>&1 thing is what redirects error (standard stream #2) to out (standard stream #1). Note that the order matters, you have to do that after redirecting out to the file.

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

2 Comments

I just could not find the duplicate for that. But of course this is the way.
java -jar xyz.jar > the_file_to_output 2>&1 This helped me.
2

Java works as any other command.

If you want standard output (or error output) you can work with usual I/O operators: > 2> >> |

java -jar xyz.jar > output.file
java -jar xyz.jar | sort | less

For getting System.exit value you have special variable $?

java -jar xyz.jar
echo $?

To get more info Exit status and I/O redirection

1 Comment

@AKC: $? has nothing to do with capturing output. If it's the exit code you wanted, though, that's what you need. (Don't forget to mark an answer correct if it answers your question.)

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.