views:

6943

answers:

5

I use the Eclipse IDE to develop, compile, and run my Java projects. Today, I'm trying to use the java.io.Console class to manage output and, more importantly, user input.

The problem is that System.Console() returns null when an application is run "through" Eclipse. Eclipse run the program on a background process, rather than a top-level process with the console window we're familiar with.

Is there a way to force Eclipse to run the program as a top level process, or at least create a Console that the JVM will recognize? Otherwise, I'm forced to jar the project up and run on a command-line environment external to Eclipse.

Thanks, Ross

+2  A: 

As far as I can tell, there is no way to get a Console object from Eclipse. I'd just make sure that console != null, then JAR it up and run it from the command line.

perimosocordiae
+1  A: 

The reason this occurs is because eclipse runs your app as a background process and not as a top-level process with a system console.

Heath Borders
+2  A: 

Found something about this at http://www.stupidjavatricks.com/?p=43 .

And sadly, since console is final, you can't extend it to create a a wrapper around system.in and system.out that does it either. Even inside the eclipse console you still have access to those. Thats probably why eclipse hasn't plugged this into their console yet...

I understand why you wouldn't want to have any other way to get a console other than System.console, with no setter, but i don't understand why you wouldn't want someone to be able to override the class to make a mock/testing console...

John Gardner
A: 

There seems to be no way to get a java.io.Console object when running an application through Eclipse. A command-line console window is not opened with the application, as it is run as a background process (background to Eclipse?). Currently, there is no Eclipse plugin to handle this issue, mainly due to the fact that java.io.Console is a final class.

All you can really do is test the returned Console object for null and proceed from there.

Ross
+7  A: 

I assume you want to be able to use step-through debugging from Eclipse. You can just run the classes externally by setting the built classes in the bin directories on the JRE classpath.

java -cp workspace\p1\bin;workspace\p2\bin foo.Main

You can debug using the remote debugger and taking advantage of the class files built in your project.

In this example, the Eclipse project structure looks like this:

workspace\project\
                 \.classpath
                 \.project
                 \debug.bat
                 \bin\Main.class
                 \src\Main.java

1. Start the JVM Console in Debug Mode

debug.bat is a Windows batch file that should be run externally from a cmd.exe console.

@ECHO OFF
SET A_PORT=8787
SET A_DBG=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=%A_PORT%,server=y,suspend=y
java.exe %A_DBG% -cp .\bin Main

In the arguments, the debug port has been set to 8787. The suspend=y argument tells the JVM to wait until the debugger attaches.

2. Create a Debug Launch Configuration

In Eclipse, open the Debug dialog (Run > Open Debug Dialog...) and create a new Remote Java Application configuration with the following settings:

  • Project: your project name
  • Connection Type: Standard (Socket Attach)
  • Host: localhost
  • Port: 8787

3. Debugging

So, all you have to do any time you want to debug the app is:

  • set a break point
  • launch the batch file in a console
  • launch the debug configuration

You can track this issue in bug 122429.

McDowell