AKC
AKC

Reputation: 1033

How to Capture Java -jar command's OS Output in Linux?

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?

Upvotes: -1

Views: 3539

Answers (2)

Vicente Vara
Vicente Vara

Reputation: 56

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

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1075885

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.

Upvotes: 5

Related Questions