Why is System.console () installed if running with java and canceled if running through ant?

I wrote a small batch application in Java and wanted to use the new java.io.Console class for this. I use System.console () to get an instance of this class. This call returns the working console if I call my application through "java -jar MyApp.jar", but does not work if I execute the application through the ant java task. fork true and splan false for this call. Why is this difference (System.out.print () works fine in ant)? How can I use the Console also if I run my application through ant?

+4
source share
4 answers

Javadoc for this method claims:

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

And the docs for the System.Console class state:

Whether the virtual machine is a console depends on the underlying platform, as well as on how the virtual machine is called. If the virtual machine is started from the interactive command line without redirecting standard input and output streams, then its console will exist and, as a rule, will be connected to the keyboard and display from which the virtual machine was started. If the virtual machine starts automatically, for example, using the background task scheduler, then usually there will be no console.

I would suggest that when Ant opens a new Java process, it redirects standard output.

+7
source

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

+2
source

Well, ant is a build automation tool. Typically, interactive applications have virtually no place in building automation, so it’s not entirely unexpected that you won’t get a console when starting tasks through ant.

+1
source

It looks like the java ant task is using javaw.exe instead of java.exe. There is no console attached to it in javaw.

0
source

All Articles