0

我已经安装了 JDK 1.6 版,现在代码已编译但会引发运行时错误

class Console1
{
    public static void main(String args[])
    {
        System.console().readLine("this is how we give he input to the string");
        System.out.println("this is what we want to print:0)");
    }
}

输出

exception in thread "main" java.lang.NoClassDefFoundError: Console
4

3 回答 3

2

看起来您正在使用不同版本的 Java 运行。将此作为第一行代码添加到您的“应用程序”中。它将显示用于运行应用程序的 java 版本。

System.out.println(System.getProperty("java.version"));
于 2012-05-20T08:21:23.387 回答
1

Instead of console you can use...

BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();

see example

于 2012-05-20T08:02:41.350 回答
1

我看到这与您的其他线程有关。

System.console() 返回控制台对象,它是在 JDK 1.6 中添加的,您说您已经安装了 jdk 1.6,因此只能假设您用于编译和运行代码的 java 版本不同。

你能告诉我你用什么来编译代码,以及你是如何运行代码的吗?

以后,请使用您最初发布的同一线程,这样更容易被人们追踪。

于 2012-05-20T07:49:12.370 回答