报错 咋办

报错 咋办

Exception in thread "main" java.lang.UnsupportedClassVersionError: net/cnns/Homework has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)

at java.net.URLClassLoader.access$100(URLClassLoader.java:73)

at java.net.URLClassLoader$1.run(URLClassLoader.java:368)

at java.net.URLClassLoader$1.run(URLClassLoader.java:362)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:361)

at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

问题原因:可能是在较低版本的JVM上运行较高版本的JDK编译的java class文件产生的错误。

解决办法:右击项目-->properties-->java compiler --> Enable project specific settings -->将compiler compliance level设置为与jvm一致的版本(jvm在命令行中java -version所显示的版本)

  • JackHe97 提问者 #1
    谢谢 弄好了
    2018-05-14 15:19:46
  • JackHe97 提问者 #2
    Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1371) at net.cnns.Homework.main(Homework.java:70) 这个咋办
    2018-05-14 16:17:17
  • irista23 回复 提问者 JackHe97 #3
    你程序是不是多次调用了scanner类的close方法?导致无法读入
    2018-05-14 16:21:30
提问者 JackHe97 2018-05-14 16:44:05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
 
public class Homework {
    // 插入数据
    public int[] insertDate() {
        int[] array = new int[10];
        int test;
        Scanner scan = new Scanner(System.in);
        // 循环插入数据
        for (int i = 0; i <= 8; i++) {
            System.out.println("插入第" + (i + 1) + "位数字");
            test = scan.nextInt();
            if (test == 0) {
                System.out.println("输入的数据不能为零,请重新输入");
                i--;
                continue;
            else {
                array[i] = test;
            }
        }
        scan.close();
        System.out.println("数据为:");
        for (int o:array) {
            System.out.print(array[o]+" ");
        }
        return array;
    }
 
    // 显示所有数据
    public void showDate(int[] a, int length) {
        int[] array = null;
        Homework b=new Homework();
        array=b.insertDate();
        System.out.println("数据为:");
        for (int o:array) {
            System.out.print(array[o]+" ");
        }
    }
 
    // 指定位置插入数据
    public void insertAtArray(int[] a, int n, int k) {
 
    }
 
    // 能被3整除的数据
    public void divThree(int[] a) {
 
    }
 
    // 提示信息
    public void notice() {
        System.out.println("*****************************");
        System.out.println("1--插入数据");
        System.out.println("2--显示所有数据");
        System.out.println("3--在指定位置插入数据");
        System.out.println("4--查询能被3整除的所有数据");
        System.out.println("0--退出");
        System.out.println("*****************************");
        System.out.println("请输入对应的数字进行操作:");
    }
 
    // 主方法
    public static void main(String[] args) {
        int[] array = null;
        Scanner scan2 = new Scanner(System.in);
        Homework method = new Homework();
        do {
            method.notice();
            // 输入数字,选择功能
            String n = scan2.next();
            switch (n) {
            case "1":
                array = method.insertDate();
                break;
            case "2":
                method.showDate(null0);
                // break;
                // case "3":
                // method.insertAtArray(int[] a, int n, int k);
                // break;
                // case "4":
                // method.divThree(int[] a);
                // break;
            case "0":
                System.out.println("退出程序");
                System.exit(0);
            default:
                System.out.println("输入错误。");
            }
        while (1 == 1);
 
    }
 
}


  • 你把第21行的close()代码去掉,因为Scanner的close()方法会把System.in也 关闭了,再接着在主方法中读取输入流用到System.in时就会报NoSuchElementException异常信息。
    2018-05-14 16:50:50
  • 提问者 JackHe97 回复 irista23 #2
    主方法不用添加close吗
    2018-05-14 16:52:40
  • irista23 回复 提问者 JackHe97 #3
    在主方法结束处建议添加
    2018-05-14 17:16:19
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码