System.console() returns null
Asked Answered
Q

13

90

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?

Quit answered 17/11, 2010 at 10:55 Comment(1)
Also see #26471472 I identified System.out and System.in to be sufficient for my use case and abondaned using System.console().Moravia
S
40

This is a bug #122429 of eclipse

Supplicate answered 14/12, 2011 at 13:11 Comment(0)
O
36

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 ;).

Operose answered 26/6, 2012 at 0:38 Comment(3)
Should your hand-rolled readLine method close the BufferedReader in a finally (or try-with-resources) block?Revocable
No because that would close the matching the System.in stream and no further reading could be made afterward.Operose
Yes, it's odd that this stream can be closed; I expected it to be a no-op.Revocable
H
16

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.

Hughey answered 17/11, 2010 at 11:13 Comment(2)
It also returned null for me in PowerShell when running something like java -cp ... Class | tee out.Littlefield
There is a library which can be added as a dependency via Maven or Gradle that implements the idea from the article: codeberg.org/marc.nause/console-with-fallbackFunambulist
A
10

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.

Astringent answered 23/9, 2014 at 15:28 Comment(0)
W
9

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

Westhead answered 22/8, 2016 at 6:43 Comment(0)
F
8

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.

Fitton answered 17/11, 2010 at 11:1 Comment(2)
This answer lead me to find that if you are running Gradle with the deamon enabled, you will not be able to get a console in your Gradle tasks. Running my Gradle task with the ' --no-daemon' allows console access.Homogenetic
This happened to me when using drip (github.com/ninjudd/drip) for a command line tool. Oops!Topknot
W
6

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.

Wexler answered 25/6, 2018 at 20:19 Comment(0)
T
3

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

**

Towline answered 1/12, 2019 at 14:50 Comment(0)
H
2

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

Hysteroid answered 24/5, 2020 at 13:19 Comment(0)
R
1

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.

Rumrunner answered 17/11, 2010 at 11:0 Comment(0)
M
1

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

Musa answered 17/11, 2010 at 11:0 Comment(3)
yes it is only for my debug purpose, users will get shell script or bat file. Is it not gud to use console?? if yes what is the better wayQuit
BTW all remote debugging search talks abt web server, but i am not creating a web application. this is a plain java. might be better way of masking password?Quit
You can remote debug any Java process, just add e.g. -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 to the VM's arguments (on Sun JVMs at least). Using the console can be OK, but as pointed out means that your code simply won't work when launched without one. Instead, consider passing in values directly sourced from command-line arguments, system properties, property files etc. At the very least provide some alternative - e.g. only go to the console if a value wasn't provided as a command-line argument.Musa
L
0

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

Lassalle answered 18/7, 2021 at 13:33 Comment(0)
N
-1

add -console in your program arguments to start OSGi console

Nudnik answered 17/11, 2010 at 13:55 Comment(2)
can you please clarify more on this if possible an example?Quit
I learned something new today, thanks for that. However, eclipse still read the parameters from inside the internal console. so your proposed solution won't solve the original problem.Philology

© 2022 - 2024 — McMap. All rights reserved.