System.console() returns null

JavaEclipseConsoleSpringsource

Java Problem Overview


I was using readLine of BufferedReader to get input/new password from user, but wanted to mask the password so I am trying to use java.io.Console class. Problem is that System.console() returns null when an application is debugged in Eclipse. I am new to Java and Eclipse not sure is this the best way to achieve? I am right clicking on the source file and selecting "Debug As" > "Java Application". Is there any workaround?

Java Solutions


Solution 1 - Java

This is a bug #122429 of eclipse

Solution 2 - Java

This code snippet should do the trick:

private String readLine(String format, Object... args) throws IOException {
	if (System.console() != null) {
		return System.console().readLine(format, args);
	}
	System.out.print(String.format(format, args));
	BufferedReader reader = new BufferedReader(new InputStreamReader(
			System.in));
	return reader.readLine();
}

private char[] readPassword(String format, Object... args)
		throws IOException {
	if (System.console() != null)
		return System.console().readPassword(format, args);
	return this.readLine(format, args).toCharArray();
}

While testing in Eclipse, your password input will be shown in clear. At least, you will be able to test. Just don't type in your real password while testing. Keep that for production use ;).

Solution 3 - Java

System.console() returns null if there is no console.

You can work round this either by adding a layer of indirection to your code or by running the code in an external console and attaching a remote debugger.

Solution 4 - Java

I also ran into this problem when trying to write a simple command line application.

Another alternative to creating your own BufferedReader object from System.in is to use java.util.Scanner like this:

import java.util.Scanner;

Scanner in;
in = new Scanner(System.in);

String s = in.nextLine();

Of course this will not be a drop-in replacement to Console, but will give you access to a variety of different input functions.

Here's more documentation on Scanner from Oracle.

Solution 5 - Java

According to the API:

"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."

Solution 6 - Java

According to the docs:

> If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

Solution 7 - Java

Got this error message when running the application from Netbeans. Judging from the other answers, it seems this happens when running your application from an IDE. If you take a look at this question: Trying to read from the console in Java, it is because

> Most IDEs are using javaw.exe instead of java.exe to run Java code

The solution is to use the command line/terminal to get the Console.

Solution 8 - Java

I refered&used formixian's answer shown above. The point is use (black) cmd console to run your Java program as "ojonugwa ochalifu" suggested.

**

Solution 9 - Java

That is right.

You will have to run the application outside of Eclipse. Look at the launcher configuration panels within Eclipse and see if you can spot the option that says to run the command in a separate JVM.

Solution 10 - Java

I believe that in the run configurations for Eclipse, you can configure whether to assign a console or not - ensure this is checked. (It's been a while since I used Eclipse so I can't give specific instructions I'm afraid).

If that doesn't work, then something that will definitely do this job is starting your application in debug mode, then connect to the process with Eclipse. Search for "eclipse remote debugging" if you're not sure how to do this.

Furthermore, in general it is a bad idea to require a console to be assigned as this very much impacts the flexibility of your application - as you've just discovered. Many ways of invoking Java will not assign a console, and your application is unusable in these instances (which is bad). Perhaps you could alternatively allow arguments to be specified on the command line. (If you're testing the console input specifically then fair enough, but it would potentially be useful for people to be able to invoke your application from scripts and/or on headless servers, so this sort of flexible design is almost always a good idea. It often leads to better-organised code, too.)

Solution 11 - Java

If your IDE uses javaw instead of java, then this issue is bound to happen as javaw is essentially java without console window.

Solution 12 - Java

Try to compile your class and run it in terminal. It works.

Solution 13 - Java

add -console in your program arguments to start OSGi console

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGaulsView Question on Stackoverflow
Solution 1 - JavaswimmingfisherView Answer on Stackoverflow
Solution 2 - JavaformixianView Answer on Stackoverflow
Solution 3 - JavaMcDowellView Answer on Stackoverflow
Solution 4 - JavaMonkeyPaperworkView Answer on Stackoverflow
Solution 5 - JavaEmad AghayiView Answer on Stackoverflow
Solution 6 - JavaVincent RamdhanieView Answer on Stackoverflow
Solution 7 - JavaOjonugwa Jude OchalifuView Answer on Stackoverflow
Solution 8 - JavaPark JongBumView Answer on Stackoverflow
Solution 9 - JavaStephen CView Answer on Stackoverflow
Solution 10 - JavaAndrzej DoyleView Answer on Stackoverflow
Solution 11 - JavaGingerBeerView Answer on Stackoverflow
Solution 12 - JavaGregory MathviewView Answer on Stackoverflow
Solution 13 - JavaKaneView Answer on Stackoverflow