Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background process in Java

I have been preparing for OCP exam in Java 7. The book I am reading I come accross something I don't understand completely.

That is,

If the JVM is invoked indirectly by IDE, or if the JVM is invoked from a background process, then the method call System.console() will fail and return null.

In what case and how do you invoke JVM from the background process?. Can someone elaborate on that? Best regards

like image 979
ucas Avatar asked Jul 16 '26 05:07

ucas


1 Answers

Suppose you are on a UNIX system, and you run a program that can ask for two operands and produce their sum. If you invoke it as:

/home/ucas> java -jar add.jar
Please enter the first summand:
42
Please enter the second summand:
17
The sum is 59.

Now suppose you run it in the background:

/home/ucas> java -jar add.jar &
java.lang.NullPointerException at Add.main(Add.java:17)
....

Looking at Add.java, you see:

Console console = System.console(); // 16
Reader  reader  = console.reader(); // 17

The process is detached from the terminal, so console will be null. The stack trace is printed to standard error, which isn't redirected.

like image 197
Eric Jablow Avatar answered Jul 18 '26 19:07

Eric Jablow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!