java ant 类编译失败,错误的类文件使ant构建失败

博主将 JDK 从 1.6 换成 1.8 后,Ant 构建时出现类编译失败问题,错误显示类文件版本不匹配。解决方案是先检查 javac 任务是否使用特定编译器,若未指定则确保 PATH 环境变量中首个 Java 目录为 1.8,且 JAVA_HOME 也指向 JDK 1.8。

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]

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

解决方案

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:

executable

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.

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值