No console object in Eclipse?

Whenever I try to run my program that tries to get the current console object, it throws an IOException. Is there some way I can read input from the Eclipse console? Here’s my code, if it helps.

public static void main(String[] arguments) throws IOException {
	    Console c = System.console();
	    if (c == null) {
	        System.err.println("No console.");
	        System.exit(1);
	    }

Thanks.

System.console() does not throw an IOException, so I don’t think that is your problem. Read from the strack trace where the exception is thrown and what is its message.

You can also use System.in to read input.

Have you got the “allocate console” checkbox enabled in the run configuration? (Under the “common” tab).

I’ve read that console() is a Java 6 thingie and maybe your version of eclipse is not up to it. Either you need the newest Eclipse or Eclipse is not yet up to date with all stuff.

Happens from time to time with a new Java version. With Java 5 Eclipse could not handle generics in the beginning.

-JAW

Yes, I have the “allocate console” box checked in the run configuration, and my Eclipse has the latest version of Java. ???

Post the full stack trace.

Do it the old fashioned way.


InputStream in = System.in;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int data;
do {
  data = in.read();
  bout.write(data);
} while(data != 13);

String input = bout.toString();

I’d strongly suggest to Buffer the input, as described in the doc (look at the Process.getErrorStream()). That is :

InputStream in = new BufferedInputStream(System.in);

:wink:

Whoops. Yes. :-[