Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is System.console() set if executed with java and unset if executed via ant?

Tags:

java

console

ant

I wrote a little commandline-application in Java and wanted to use the new class java.io.Console for this. I use System.console() to get an instance of this class. This call returns a working console, if I call my application via 'java -jar MyApp.jar' but is unset if I execute the application via the java-task of ant. fork is true and spwan false for this call. Why is this difference (System.out.print() works fine under ant)? How can I use a Console also if I start my App via ant?

like image 537
Mnementh Avatar asked Dec 30 '22 18:12

Mnementh


2 Answers

The Javadoc for this method states:

Returns the unique Console object associated with the current Java virtual machine, if any.

And the docs for the System.Console class state:

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

I would imagine that when Ant forks a new Java process it redirects standard output.

like image 145
matt b Avatar answered Feb 17 '23 00:02

matt b


System.console() returns null if input or output is redirected. Ant just does that.

like image 31
fg. Avatar answered Feb 16 '23 23:02

fg.