错误的类文件导致 ant 构建失败 [英] Bad class file failed the ant build

查看:33
本文介绍了错误的类文件导致 ant 构建失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将 jdk1.6 更改为 1.8 并且在制作新的 ant 构建时,它给出了这样的错误信息:

I just changed jdk from 1.6 to 1.8 and when making an new ant build, it gives such error messages:

[javac] /usr/workspace/test/src/JsonString.java:7: cannot access java.lang.Object
[javac] bad class file: java/lang/Object.class(java/lang:Object.class)
[javac] class file has wrong version 52.0, should be 50.0
[javac] Please remove or make sure it appears in the correct subdirectory of the classpath.
[javac] public class JsonString {
[javac] 

这很奇怪.Object 类应该在 java1.8 中,怎么会坏呢?有大佬知道怎么解决吗?

This is weird. the Object class should be in java1.8, how could it be bad? Do anyone know how to solve this?

推荐答案

首先检查 javac 任务没有使用默认编译器以外的特定编译器.如果您将 executable 属性设置为 fork 特定编译器(以及 fork="yes"),则可能会发生这种情况,如 任务文档:

First check that the javac task is not using a specific compiler other than the default one. This can occur if you're setting the executable attribute to fork a specific compiler (along with fork="yes"), as mentioned in the task documentation:

可执行

在 fork="yes" 的情况下要使用的 javac 可执行文件的完整路径.默认为当前运行 Ant 的 Java 版本的编译器.如果 fork="no" 则忽略.自 Ant 1.6 起,此属性还可用于在使用 jikes、jvc、gcj 或 sj 时指定可执行文件的路径.

Complete path to the javac executable to use in case of fork="yes". Defaults to the compiler of the Java version that is currently running Ant. Ignored if fork="no". Since Ant 1.6 this attribute can also be used to specify the path to the executable when using jikes, jvc, gcj or sj.

如果您只是调用 javac 而没有指定任何外部编译器,那么 Ant 将使用运行 Ant 的 Java 版本附带的编译器.从错误消息来看,它显然使用的是 Java 1.6 编译器(类文件的版本为 50.0).Ant 通常使用 PATH 环境变量中的 java 可执行文件运行.因此,请确保 PATH 变量中指定的第一个 Java 目录是 Java 1.8.

If you're just calling javac without specifying any external compiler, then Ant would use the compiler that comes with the Java version that is running Ant. And from the error message, it's clearly using the Java 1.6 compiler (for which classfiles have version 50.0). Ant normally runs using the java executable found in the PATH environment variable. So make sure that the first Java directory that is specified in the PATH variable is Java 1.8.

理想情况下,您应该将 JAVA_HOME 设置为 Java 1.8 的路径,并使 PATH 引用 JAVA_HOME 本身,以便两者都指向相同的安装.

Ideally you should have JAVA_HOME set to the path of Java 1.8, and have PATH refer to JAVA_HOME itself so that both point to the same installation.

JAVA_HOME -> /path_to_jdk1.8
PATH -> %JAVA_HOME%;...;/path_to_some_other_jdk

这篇关于错误的类文件导致 ant 构建失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆