can't find console method

I'm using jdk1.6.0_02 and I have my classpath set to ....jdk1.6.0_02\jre\lib\rt.jar within that installation directory. I'm trying to compile my source Password.java from the command line and I get the following error:

>javac Password.java

Password.java:13: cannot find symbol

symbol : method console()

location: class java.lang.System

Console c = System.console();

^

1 error

can somebody help me with this?

Here's the source:

import java.io.Console;

import java.util.Arrays;

import java.io.IOException;

publicclass Password{

publicstaticvoid main(String[] args)throws IOException{

Console c = System.console();

if (c ==null){

System.err.println("Console is unavailable");

System.exit(1);

}

String login = c.readLine("Login: ");

char[] oldPassword = c.readPassword("Current Password: ");

if (verify(login, oldPassword)){

boolean noMatch;

do{

char[] newPassword1 = c.readPassword("New Password: ");

char[] newPassword2 = c.readPassword("Confirm Password: ");

noMatch = !Arrays.equals(newPassword1, newPassword2);

if (noMatch){

c.format("Passwords don't match. Try Again.%n");

}else{

change(login, newPassword1);

c.format("Password for %s changed successfully.%n", login);

}

}while (noMatch);

}

}

staticboolean verify(String a,char[] b){

returntrue;

}

staticvoid change(String a,char[] b){

}

}

[3167 byte] By [thousea] at [2007-11-27 9:55:37]
# 1

> I'm using jdk1.6.0_02 and I have my classpath set to

> ....jdk1.6.0_02\jre\lib\rt.jar

Don't. You should no put rt.jar on the classpath. It will be found automatically relative to the installation.

> >javac Password.java

> Password.java:13: cannot find symbol

> symbol : method console()

> location: class java.lang.System

>Console c = System.console();

>^

console() was added in 1.6, so your javac must still be 1.5 or earlier. Try javac -version. Make sure 1.6 is in your PATH variable (NOT CLASSPATH), not 1.5.

jverda at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 2
remove all java of your pcinstall java 1.6 againuse java -version as said before to verify if the version is OK
pbulgarellia at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 3
> remove all java of your pcThis is not necessary, but if you're not planning on switching back and forth between versions, it's probably a good idea.
jverda at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 4

> ...

> console() was added in 1.6, so your javac must still

> be 1.5 or earlier. Try javac -version. Make sure 1.6

> is in your PATH variable (NOT CLASSPATH), not 1.5.

@OP:

Another possibility is that you're trying to run this code with Java 1.6 but from within your IDE, which won't work. A Console can only be created from... the console/shell.

Edit:

never mind, I mis-read: it's going wrong with compiling, so you must be using a javac pre 1.6.

prometheuzza at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 5

> Another possibility is that you're trying to run this

> code with Java 1.6 but from within your IDE,

> which won't work. A Console can only be

> created from... the console/shell.

You sure about that? Don't most IDEs have consoles that handle stdin/out?

Anyway, this error was occuring when he invoked javac.

jverda at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 6

> ...

> You sure about that? Don't most IDEs have consoles

> that handle stdin/out?

Some IDE's perhaps, but Eclipse prints true after running this code:

public class Foo {

public static void main(String[] args) {

java.io.Console c = System.console();

System.out.println(c == null);

}

}

And false if I run it from the command line.

> Anyway, this error was occuring when he invoked javac.

See my edit.

prometheuzza at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 7

> Some IDE's perhaps, but Eclipse prints true

> after running this code:

> public class Foo {

>public static void main(String[] args) {

>java.io.Console c = System.console();

>System.out.println(c == null);

> }

> }

> And false if I run it from the command line.

Interesting. Eclipse's console does behave like a command-line console in a simple test, but apparently there's a distinction. (Note that I haven't looked into the Console class at all yet.)

> See my edit.

Yeah, I saw it after I posted and couldn't be bothered to go edit my own post.

jverda at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 8

the javac is also version 1.6 - I do plan on going between versions so I don't want to delete off earlier releases. When I do run this from an IDE, it is unable to provide a console but according to the java tutorial, it should be able to access the console whether or not you're running from within the console / shell on your system.

thousea at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 9

If you read the docs for Console, you'll see it's kind of fuzzy and implementation-dependent as to when a console is actually available. Apparently the VM or some eclipse hook into it has decided that eclipse's console is not a console.

One thing just struck me though: Are you running eclipse from the command line or by double-clicking something? If the latter, it's not surprising if Windows' runtime environment decided there's no console before eclipse even got launched. If you're doing it from the command line, you might be able to convince it to act like a "real" console, thought I can't say I know how.

jverda at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 10

from within eclipse I get no console, but the bulk of my effort has gone into trying to compile this program from a DOS window using the javac provided with my JDK1.6.0_02.

I'd think that whether or not a console is available in the windows env, the code should definitely still compile, right? That's independent of the underlying platform

Message was edited by: thouse

thouse

thousea at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 11

> from within eclipse I get no console, but the bulk of

> my effort has gone into trying to compile this

> program from a DOS window using the javac provided

> with my JDK1.6.0_02.

But how do you start eclipse?

> I'd think that whether or not a console is available

> in the windows env, the code should definitely still

> compile, right? That's independent of the underlying

> platform

Right. That depends only on the version of the JDK. So if it compiles in one situation and not the other, then the two cases are using two different versions of the JDK.

jverda at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 12
I don't start Eclipse from the command line - I'm just opening the exe from within Windows - are you wondering whether the console will only be available when Eclipse is launched from a console?
thousea at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...
# 13

> I don't start Eclipse from the command line - I'm

> just opening the exe from within Windows - are you

> wondering whether the console will only be available

> when Eclipse is launched from a console?

I was speculating that it might be available if eclipse is started from a console. If you're starting it by doubl clicking, I'm definitely not surprised that there's no console available.

jverda at 2007-7-13 0:25:37 > top of Java-index,Java Essentials,New To Java...