0

I have a JVM app that is being run from a bash script. I would like for the app to return an output to the script, so that the script can use it as a parameter for other commands.

One suggestion I've read is to use System.out.print on the desired output. However, my application does a significant amount of logging using log4j. It also invokes other libraries which also log other info as well. If my bash-script tries to read from stdout, wouldn't it read all of those log-outputs as well?

Another option I thought of is:

  1. The script passes in a /tmp/${RANDOM}.out file-path to the application
  2. The JVM application writes the desired output to the specified file
  3. The script reads the value off the specified file, once the application has finished running

The above approach seems more cumbersome, and makes certain assumptions about the system's file-system and write-permissions. But it's the best option I can think of.

Is there a better way to do this?

1 Answer 1

1

This answer assumes some experience and knowledge about bash IO channels, Java out/err print, and log4j configuration files.

There are 2 techniques: 1. In the Java file, send relevant output to System.err. In the bash script, capture channel 2 which is stderr. Channel 1 is stdout. 2. In the log4j config .xml, use a file appender. This will send logging data to a file. Define the Logger Root to use this appender.

There are many subtleties that can affect these techniques. Hopefully, one of these options will suffice.

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

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.