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?
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.
System.console() returns null if input or output is redirected. Ant just does that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With