Bug 122429 - System.console() (Java 6.0) returns null when running inside Eclipse
Summary: System.console() (Java 6.0) returns null when running inside Eclipse
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Debug (show other bugs)
Version: 3.2   Edit
Hardware: All All
: P3 enhancement with 77 votes (vote)
Target Milestone: ---   Edit
Assignee: JDT-Debug-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 179197 295177 342377 559834 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-12-31 23:39 EST by Willian Mitsuda CLA
Modified: 2021-09-17 03:34 EDT (History)
48 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Willian Mitsuda CLA 2005-12-31 23:39:36 EST
Java 6.0 (mustang) provides a new class to represent the system console (http://download.java.net/jdk6/docs/api/java/io/Console.html)

The documentation (http://download.java.net/jdk6/docs/api/java/lang/System.html#console()) says: "Returns the unique Console object associated with the current Java virtual machine, if any".

When I run the following code:

import java.io.Console;

public class Main {

	public static void main(String[] args) {
		Console console = System.console();
		System.out.println(console);
	}

}

directly using "java Main", it works (console != null). However, when I run inside Eclipse (Alt+Shift+X J), it returns null.

I'm not sure if this is something must be handled at Eclipse level, or at JRE level (or both...), or it should be handled now during the development phase of Java 6.0 (besides bug#106206).
Comment 1 Willian Mitsuda CLA 2005-12-31 23:42:59 EST
One more thing: I'm running Eclipse 3.2 M4, with jdk1.6.0 build 65.
Comment 2 Martin Aeschlimann CLA 2006-01-04 09:24:49 EST
In one case the Java command is started out of a shell (-> there is a console), in the other (the Java application launcher) it is started directly using a process (no console).

The spec says: "Returns the unique Console object associated with the current Java virtual machine, if any".
'If any' means that you can also get null. This look all ok to me.
Comment 3 Willian Mitsuda CLA 2006-01-04 09:43:03 EST
OK, I agree that it is possible to get a null from System.console() when ***there is no console***.

But, what about the console view? Shouldn't it be considered a console? ;-)

I ask you: how will I run a project that uses java.io.Console in Eclipse?

The intuitive way is to expect that once I run it via java application launcher, the System.console() will return a console associated with the console view, as is when you use System.out (writes to console view) or System.in (reads input from console view).

Don't you agree with me?
Comment 4 Alex Blewitt CLA 2007-04-11 08:05:59 EDT
It seems strange that a process can print out to System.out.println() and have it display in the Console view (as well as read input via System.in) and for the Console to be null. I'm sure this will come back to bite as various Java processes start to use java.io.Console for the future.
Comment 5 Darin Wright CLA 2007-04-23 14:50:37 EDT
*** Bug 179197 has been marked as a duplicate of this bug. ***
Comment 6 Dominik Goepel CLA 2007-04-24 10:59:55 EDT
maybe this could be reopened as an enhancement request for 3.3 (or beyond)? 
I agree with Alex, this will be noticed by many people as soon as System.console() adoption increases. 
Comment 7 Willian Mitsuda CLA 2007-04-24 13:31:21 EDT
Given the recent discussion, I'm reopening as a enhancement request.

I think the problem here is how to plug the Eclipse console into the JRE 6 notion of console.

Suggestions?
Comment 8 Darin Wright CLA 2007-04-24 13:59:39 EDT
Moving to debug... the console is associated with running/debugging.
Comment 9 Genady Beryozkin CLA 2007-04-24 14:26:21 EDT
The istty() method has the following check:

if (GetFileType(hStdIn) != FILE_TYPE_CHAR ||
        GetFileType(hStdOut) != FILE_TYPE_CHAR)
        return JNI_FALSE;

In order for the method to return true the type of the redirected streams should be changed (I don't think there's an api for that). In addition Eclipse should support turning the console echo on and off using just the redirected io streams.

Another relevant discussion is at http://blogs.msdn.com/junfeng/archive/2005/07/07/436746.aspx
Comment 10 Darin Wright CLA 2007-04-24 14:44:25 EDT
Marking as assigned for future consideration/investigation. It's too late for 3.3.
Comment 11 Gary Gregory CLA 2008-03-06 02:03:43 EST
How about for 3.4?
Comment 12 Darin Wright CLA 2008-03-06 09:12:07 EST
Sorry, nothing planned for 3.4. Not sure if an API changes would be required - but the M6/API freeze is coming soon. If anybody wants to contribute a patch, please do so.
Comment 13 Eric Bresie CLA 2008-05-19 14:08:28 EDT
How about a new "Run As" option which would have "Console Application".  It could then spawn a command prompt which would then run correctly, avoiding extra internal logic.  

However are there any security concerns that doing it this way might drum up?

In the "Common" tab there is a "Standard Input and Output" which has "Allocate Console" checkbox.  Is this in any way related to this?  Or the "Launch in background" checkbox?
Comment 14 Darin Wright CLA 2008-05-20 09:46:52 EDT
(In reply to comment #13)
> In the "Common" tab there is a "Standard Input and Output" which has "Allocate
> Console" checkbox.  Is this in any way related to this?  Or the "Launch in
> background" checkbox?

No. This is just whether a console view should be allocated in the SDK for the I/O. You can have output shown in the console, sent to a file, or both.
Comment 15 McDowell CLA 2008-09-20 08:52:38 EDT
Developers can work round this limitation using remote debugging as described here: http://stackoverflow.com/questions/104254/javaioconsole-support-in-eclipse-ide#105403

With this workaround, it is possible to reduce application launch to a two-step process. Whether it is worth automating depends on java.io.Console uptake, I guess.

It would be possible to add a new launch configuration for Java applications that reused the existing launch extensions implementation code. It might be best to provide an edit box to build the launch command to avoid having to cater for every potential console out there (though supporting ${workspace_loc:/}-style variables and providing for common defaults would help).

On Windows, it is possible to spawn a console window from Eclipse using a command like:

cmd.exe /C start cmd.exe /C java.exe -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y -cp .\bin Main

On *nix variants, it would be possible to spawn a console process using xterm. These are easy to test using the External Tools dialog.
Comment 16 Sean Kennedy CLA 2008-10-29 17:48:10 EDT
We are using Console.readPassword(): my expectation was that Eclipse would handle this by use the console view.
More people are using Console, and are all expecting Eclipse to connect the console view to facilitate it.
It is time to make this work.
Comment 17 Darin Wright CLA 2008-10-29 22:38:14 EDT
From what I understand, there is no a way for Eclipse to connect the console view to Java's System.console():

http://www.eclipsezone.com/eclipse/forums/t93685.rhtml

The simplest solution might be to provide on option to launch a Java app from a command shell (configured via the common tab console options). However, I doubt there is a platform independent way to do that either.
Comment 18 Remy Suen CLA 2008-10-30 02:18:50 EDT
(In reply to comment #17)
> The simplest solution might be to provide on option to launch a Java app from a
> command shell (configured via the common tab console options).

I hear this complaint/request all the on IRC.

> However, I doubt there is a platform independent way to do that either.

...but yeah, I dunno either.

Comment 19 mathew CLA 2008-12-09 18:32:50 EST
I just got confused by this issue.

I suggest that if the Console window in Eclipse cannot be made to behave as a Console, that it should be renamed the Output window.
Comment 20 Darin Wright CLA 2008-12-09 22:56:35 EST
(In reply to comment #19)
> I just got confused by this issue.
> I suggest that if the Console window in Eclipse cannot be made to behave as a
> Console, that it should be renamed the Output window.

I'm pretty sure that would confuse more people since the console view has been named this since Eclipse was born, which was before Java 6 arrived on the scene.

Comment 21 Remy Suen CLA 2008-12-10 00:54:55 EST
(In reply to comment #20)
> I'm pretty sure that would confuse more people since the console view has been
> named this since Eclipse was born, which was before Java 6 arrived on the
> scene.

Agreed. That wouldn't really help. Besides, the view also allows input and I think 'IO View', 'I/O View', and/or 'Input/Output View' all sound pretty dumb.
Comment 22 Scott Reed CLA 2009-03-24 15:35:06 EDT
(In reply to comment #13)
> In the "Common" tab there is a "Standard Input and Output" which has "Allocate
> Console" checkbox.  Is this in any way related to this?  Or the "Launch in
> background" checkbox?

In 3.4, if not earlier versions, this checkbox label says "Allocate Console (necessary for input)"! If that's not referring to System.console() input, what input is it referring to? That label certainly supported my expectations for having a working input console.

Comment 23 Darin Wright CLA 2009-03-24 16:14:50 EDT
> what input is it referring to?

The input stream of the associated java.lang.Process.
Comment 24 Steven Buroff CLA 2009-05-16 14:50:11 EDT
I have to use Console because I need the readPassword() function. That means I
can't use eclipse to test or debug my application. It seem very inconsistent
that System.out and System.in work and Console doesn't. I assume this is a figment
of the implementation since no one would simply decide not to support Console.
It certainly violates the principle of "minimal surprise". Please add me to the
list of people who would like to see this bug fixed ASAP. (Since eclipse 4.2 is
supposed to support Java 1.6, I would call this a bug fix, not an enhancement).
Thanks.
Comment 25 Olivier Thomann CLA 2009-11-15 20:46:15 EST
*** Bug 295177 has been marked as a duplicate of this bug. ***
Comment 26 Arthur Johnson CLA 2010-04-01 11:31:39 EDT
Can we please put this at a Severity of Blocker? It is an impediment to testing code to do with passwords and security.
Comment 27 Darin Wright CLA 2010-04-05 17:28:21 EDT
No one has offered a tried/tested solution to this problem. There does not appear to be an API to set the system console for a VM. Perhaps a workaround would be to launch a DOS shell and launch java from there. I have found that "cmd.exe /c start" will open a command shell, but I'm not sure how to then exec a command. Perhaps a DOS guru could do some more digging here.
Comment 30 Eric Bresie CLA 2010-04-05 22:10:09 EDT
Would developing some kind of terminal emulator layer via some "serial" interface be a means to resolve this?  Then have the "Console" utilizing this?
Comment 31 Darin Wright CLA 2010-04-05 22:37:16 EDT
(In reply to comment #30)
> Would developing some kind of terminal emulator layer via some "serial"
> interface be a means to resolve this?  Then have the "Console" utilizing this?

The problem is that we don't know how to *set* the console when launching a VM. We have a console that supports reading output/error stream and writing to sysin. It's not clear if there's a way to connect it to the VM's system console.
Comment 32 weston.pace CLA 2010-04-06 15:03:20 EDT
(In reply to comment #31)
> (In reply to comment #30)
> > Would developing some kind of terminal emulator layer via some "serial"
> > interface be a means to resolve this?  Then have the "Console" utilizing this?
> 
> The problem is that we don't know how to *set* the console when launching a VM.
> We have a console that supports reading output/error stream and writing to
> sysin. It's not clear if there's a way to connect it to the VM's system
> console.

Java 6 API:
"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."

One solution would be for to Eclipse to start a server on some port.  When a console is invoked it would execute a new instance of the JVM, pipe the stdout of the JVM instance to another Java client program which would then communicate with the Eclipse server program.  I don't know enough about the Eclipse infrastructure but that doesn't sound like a fun task.
Comment 33 XIMES CLA 2010-10-28 21:13:52 EDT
As I falled in this page while looking for the problem, a quick solution for reading from keyboard is:

BufferedReader d = new BufferedReader(new InputStreamReader(System.in));

as suggested in DataInputStream.readLine() deprecated method.
Comment 34 Eric Bresie CLA 2010-10-29 15:42:18 EDT
Not sure if these are related but I was looking through some java bugs and saw the following:

- http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4109888
- http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4062587 

The 4062587 includes a "workaround" for what may be a similar problem.  See the end of the bug.

On a side note, Netbeans seems to be able to open a console, run the program, leave a "Press Enter" type prompt to prevent closure of the console.
Comment 35 Ranjeet CLA 2011-03-19 08:24:22 EDT
I want to read the console input and then i want to display that in my web page can any buddy help me for that(i used servlet to devlop my web page in java) 


Thanks in advance
Comment 36 Dani Megert CLA 2011-04-11 05:52:32 EDT
*** Bug 342377 has been marked as a duplicate of this bug. ***
Comment 37 Douglas Pollock CLA 2012-02-10 13:59:27 EST
I just bumped into this too.  It's definitely a bit of a surprise.  The issue is that the view is called "Console" and that the launch configuration specifically asks "Allocate console (necessary for input)".

Reading through Sun's source code for Console, it seems like you should be able to fix this by creating your own instance of JavaIOAccess (or somehow finding a way to override FileDescriptor.in).  I assume there is a similar way to fix it for IBM's VM.

A solution here doesn't have to be perfect across all VMs, but a best-effort would be better than the current state.  Don't let perfection by the enemy of the good!  :)
Comment 38 Mauro Molinari CLA 2012-03-14 11:33:12 EDT
(In reply to comment #37)
> I just bumped into this too.  It's definitely a bit of a surprise.  The issue
> is that the view is called "Console" and that the launch configuration
> specifically asks "Allocate console (necessary for input)".

Totally agree with this.
Comment 39 Will Warren CLA 2012-12-23 10:28:04 EST
I too need to test a tool that depends on the Console.readPassword
Comment 40 Andrew Case CLA 2013-09-05 23:22:33 EDT
Hmm... 7 years without any progress on this?  This seems like a pretty fundamental problem.  Do any other IDE's have solutions for this?
Comment 41 Menno Jansz CLA 2014-01-29 10:10:38 EST
I agree this needs fixing.
Comment 42 Michael Osipov CLA 2014-12-18 08:00:48 EST
Any move yet?
Comment 43 Murugesh Naidu CLA 2014-12-29 16:14:37 EST
Is this fixed yet? Can definitely use this right now.
Comment 44 G. Zsombor CLA 2015-02-26 18:59:37 EST
9 years passed since this bug found, and no solution yet.
Comment 45 Stephan Herrmann CLA 2015-02-27 20:49:55 EST
(In reply to G. Zsombor from comment #44)
> 9 years passed since this bug found, and no solution yet.

Opinions seem to converge at saying that there simply isn't a solution, due to the design of the Java runtime libraries.


That said, I made a naive experiment, starting from the javadoc of java.lang.Process, where it says:

"By default, the created subprocess does not have its own terminal or console."

This would be end of story, except for the nice "by default". A little later:

"Where desired, subprocess I/O can also be redirected using methods of the ProcessBuilder class".

Here's my experiment: inside org.eclipse.debug.core.DebugPlugin.exec(String[], File, String[]) I replaced this

    p= Runtime.getRuntime().exec(cmdLine, envp, workingDirectory);

with this:

    ProcessBuilder processBuilder = new ProcessBuilder(cmdLine);
    if (envp != null) {
        // populate processBuilder.environment()
    }
    processBuilder = processBuilder.directory(workingDirectory);
    processBuilder.redirectInput(Redirect.INHERIT);
    processBuilder.redirectOutput(Redirect.INHERIT);
    processBuilder.redirectError(Redirect.INHERIT);
    p = processBuilder.start();

Guess what: after installing the modified plugin, my test application could happily acquire a System.console() and print to - guess where -
the console from where Eclipse has been started.

Obviously, this requires that Eclipse is indeed started from some console (a linux shell in my case).

My guess is, you can only have one:
1) use System.console(), or
2) use the Console view in Eclipse.
Take your choice!

I wonder, whether people would be helped already, if there was an option in the debug UI, s.t. like:
 [x] Inherit console from where Eclipse has been launched
The effect would be simpler than launching outside in debug mode and having to connect a remote debug session. But still users would have to hunt for the underlying console, and what if Eclipse was launched from some shortcut icon on the desktop or elsewhere?

At least it would be platform independent.

Would people consider this approach worth pursuing?
Comment 46 T. Orf CLA 2015-02-28 12:43:15 EST
(In reply to Stephan Herrmann from comment #45)
> Would people consider this approach worth pursuing?

Yes, that would be much better than null. 

Under Windows, would the terminal around eclipse have to be the default cmd.exe, or would better terminals like cygwin mintty work too?
Comment 47 Paul Pazderski CLA 2020-02-04 15:48:47 EST
*** Bug 559834 has been marked as a duplicate of this bug. ***
Comment 48 Iain Davis CLA 2021-09-16 19:37:59 EDT
2021 and I'd sure still like to see a satisfactory solution to this bug.

Currently between jobs and trying to self-teach a bit about the Java Security architecture, and this is an unfortunate roadblock that is going to unnecessarily divert me and send me down a rabbit hole researching work-arounds for an only-tangentially-related problem that has apparently existed since 2005 without adequate resolution. 16 years is an absolute epoch in software-time, and it's not as though Security is some little-used, mostly-unnecessary subsystem.

Please fix this.
Comment 49 Sarika Sinha CLA 2021-09-17 03:34:03 EDT

(In reply to Iain Davis from comment #48)
> 2021 and I'd sure still like to see a satisfactory solution to this bug.
> 
> Currently between jobs and trying to self-teach a bit about the Java
> Security architecture, and this is an unfortunate roadblock that is going to
> unnecessarily divert me and send me down a rabbit hole researching
> work-arounds for an only-tangentially-related problem that has apparently
> existed since 2005 without adequate resolution. 16 years is an absolute
> epoch in software-time, and it's not as though Security is some little-used,
> mostly-unnecessary subsystem.
> 
> Please fix this.

I hope you understand that there are limitations from JVM.
Please go through https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429#c45 

Which option will work for you?