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