Accepted answer

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:


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. you can work round this issue in your application by using an abstraction layer as described here.


let's say your eclipse workspace is c:\myworkspace, you created your java application inside a maven project myproject, and your java main class is com.mydomain.mypackage.myclass.

in this case, you can run your main class that uses system.console() on the command line:

java -cp c:\myworkspace\myproject\target\classes com.mydomain.mypackage.myclass

nb1: if it's not in a maven project, check the output folder in project properties | java build path | source. it might not be "target/classes"

nb2: if it is a maven project, but your class is in src/test/java, you'll likely have to use "target\test-classes" instead of "target\classes"


there seems to be no way to get a 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 is a final class.

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


this link offers alternatives to using system.console(). one is to use a bufferedreader wrapped around, the second is to use a scanner wrapped around

neither are as concise as console, but both work in eclipse without having to resort to debug silliness!


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.


found something about this at .

and sadly, since console is final, you can't extend it to create a a wrapper around 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...


another option is to create a method to wrap up both options, and "fail over" to the method when console isn't available. the below example is a fairly basic one - you can follow the same process to wrap up the other methods in console (readpassword, format) as required. that way you can run it happily in eclipse & when its deployed you get the console features (e.g. password hiding) kicking in.

    private static string readline(string prompt) {
        string line = null;
        console c = system.console();
        if (c != null) {
             line = c.readline(prompt);
        } else {
            bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(;
            try {
                 line = bufferedreader.readline();
            } catch (ioexception e) { 
        return line;


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.


you can implement a class yourself. following is an example:

public class console {
    bufferedreader br;
    printstream ps;

    public console(){
        br = new bufferedreader(new inputstreamreader(;
        ps = system.out;

    public string readline(string out){
            return br.readline();
        }catch(ioexception e)
            return null;
    public printstream format(string format, object...objects){
        return ps.format(format, objects);


the workaround that i use is to just use instead of console when using eclipse. for example, instead of:

string line = system.console().readline();

you can use:

bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(;
string line = bufferedreader.readline();

Related Query

More Query from same tag