我正在使用sun / oracle jdk 1.6.0_32并且javac编译器似乎为目标1.5 jvm而不是1.6创建类文件。它不会接受-source 1.6 -target 1.6。用javap查看生成的类文件显示了一个主要版本:49不是50,正如我所料。 Eclipse Indigo正确创建了1.6兼容的类文件,主要版本为:50。我在eclipse中使用ant 1.8.2时首先注意到这一点,并且无法将源或目标属性设置为1.6。有什么我做错了吗?
附录
我只安装了1个jdk(1.6.0_32)。从命令行我得到以下内容。
ken@kryten:~/projects/simpleHelloWorld/bin/com/kwcons$ ls -al
total 8
drwxr-xr-x 2 ken ken 4096 2012-06-28 16:50 .
drwxr-xr-x 3 ken ken 4096 2012-06-28 16:46 ..
ken@kryten:~/projects/simpleHelloWorld/bin/com/kwcons$ javac -d /home/ken/projects/simpleHelloWorld/bin -version /home/ken/projects/simpleHelloWorld/src/com/kwcons/HelloWorld.java
javac 1.6.0_32
ken@kryten:~/projects/simpleHelloWorld/bin/com/kwcons$ ls -al
total 12
drwxr-xr-x 2 ken ken 4096 2012-06-28 16:50 .
drwxr-xr-x 3 ken ken 4096 2012-06-28 16:46 ..
-rw-r--r-- 1 ken ken 441 2012-06-28 16:50 HelloWorld.class
ken@kryten:~/projects/simpleHelloWorld/bin/com/kwcons$ /usr/lib/jvm/java-6-sun/bin/javap -verbose HelloWorld |grep major
major version: 49
ken@kryten:~/projects/simpleHelloWorld/bin/com/kwcons$
如果我使用-source和-target,这就是我得到的。
ken@kryten:~/projects/simpleHelloWorld/bin/com/kwcons$ javac -d /home/ken/projects/simpleHelloWorld/bin -version -source 1.6 -target 1.6 /home/ken/projects/simpleHelloWorld/src/com/kwcons/HelloWorld.java
javac 1.6.0_32
javac: invalid source release: 1.6
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files
-cp <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
ken@kryten:~/projects/simpleHelloWorld/bin/com/kwcons$
我在eclipse中得到了与ant相同的输出。
答案 0 :(得分:0)
试试这个 1)在ant脚本中指定jdk版本
2)在你的“编译”目标中写下以下javac目标,
runtime-libs ::它将指向您的项目lib目录。在属性中声明。
<javac
bootclasspath="${runtime-libs}/rt.jar"
target="1.6"
source="1.6"
includeJavaRuntime="false"
includeAntRuntime="false"
debug="yes"
destdir="${build-dir}">
<classpath refid="project.class.path"/>
<src path="${code}"/>
</javac>