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

2808

被折叠的 条评论
为什么被折叠?



