1

I want to read password from console in one my java program. I used

 Console newConsole = System.console();

But I could able to declare and initialize console object in my class. I am getting an error message

" the method console() is undefined for the type System" and " Console cannot be resolved or is not a type"

Hence I thought I don't have latest java version and checked my java version using

C:\>java -version
    java version "1.7.0_21"

I came to know that I have java 1.7.0_21 which is the latest version but still I am getting error. Anybody could you please help me regarding this.

If I can't use System.console then are there any other method I can use to read password as hidden character from console.

The complete code :

  import java.io.console;

  public class Test {

     public static void main(String[] args) {

            Console newConsole = System.console();

    }
   }

I am getting compiler error in import java.io.console (java.io.console cannot be resolved) and Console newConsole = System.console(); (the method console() is undefined for the type System and Console cannot be resolved or is not a type) line.

Anup
  • 265
  • 2
  • 6
  • 14
  • 2
    Did you add the import statement for `java.io.Console`? – MadProgrammer Aug 13 '13 at 01:43
  • Please post _all_ of your code for that particular file. – Chris Thompson Aug 13 '13 at 01:44
  • 1
    This Should help. http://stackoverflow.com/questions/15159762/how-to-read-password-from-console-without-using-system-console – JNL Aug 13 '13 at 02:07
  • Are you using an IDE? Is the compiler the same version as the interpreter? – tbodt Aug 13 '13 at 02:40
  • @MadProgrammer - Thank you for your response. yes I have used the import statement. I am also getting error on that statement. – Anup Aug 13 '13 at 02:48
  • @Chris Thompson - Thank you for your response. I have edited and added the code in my question. – Anup Aug 13 '13 at 02:49
  • @tbodt - Thank you for your response. I am using SAP Netweaver developer studio IDE. I did not check the version of interpreter. I will check it and add comment on that.. – Anup Aug 13 '13 at 02:57

3 Answers3

1

according to javadoc:

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. 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.

criszhao
  • 134
  • 6
1

You want import java.io.Console instead of import java.io.console.

import java.io.Console;

public class Test {
     public static void main(String[] args) {
            Console newConsole = System.console();
     }
}

That should get it to compile, but what I've found says System.console() will return null inside an IDE and to check this link out.

Eli Rose
  • 6,788
  • 8
  • 35
  • 55
0

Just wanted to add that error coming from wrong import would be

error: The import java.io.console cannot be resolved
    import java.io.console;

error: Console cannot be resolved to a type
    Console console = System.console();

So by fixing a typo as suggested

import java.io.Console;

public class Test {
   public static void main(String[] args) {
          Console newConsole = System.console();
   }
}

Will not solve other problem, that is

the method console() is undefined for the type System" and " Console cannot be resolved or is not a type"

I accidentally get this error working in Kubuntu 14.04 with pre-installed javac compiler that is actually

gcj-4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4

And that compiler is the one that causes problem above.

When I realized that I was not actually using javac from JDK package, and typed to compile

$JAVA_HOME/bin/javac 

That actually worked, because that compiler was actually from JDK that I've installed, and not GNU Java Compiler that was the one causing the error

I could not find fast information about packages in GNU compiler, but I know that sometime ago they did not have System.console() in some releases.

One can take a look here, that was re-directed from here

The question is so bearded now, and may be in new releases this error should not occur.

But I however pretty sure that error method console is undefined is coming not because of import errors or typos but rather from the compiler that does not know where to find System.console() method.

And although java version was printed and looks correct

C:>java -version java version "1.7.0_21"

It does not matter what java version it is, rather what compiler javac was used to compile