Mnementh
Mnementh

Reputation: 51311

Why is System.console() set if executed with java and unset if executed via 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?

Upvotes: 4

Views: 956

Answers (4)

Cooper
Cooper

Reputation: 1340

It looks like the ant java task is using javaw.exe instead of java.exe. javaw doesn't have a console attached to it.

Upvotes: 0

Joey
Joey

Reputation: 354694

Well, ant is a build automation tool. Usually interactive applications have little to no place within build automation, so it's not entirely unexpected that you won't get a console when running tasks through ant.

Upvotes: 1

fg.
fg.

Reputation: 331

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

Upvotes: 2

matt b
matt b

Reputation: 139971

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.

Upvotes: 7

Related Questions