我正在使用 sun/oracle jdk 1.6.0_32,javac 编译器似乎为目标 1.5 jvm 而不是 1.6 创建类文件。它不接受 -source 1.6 -target 1.6。用 javap 查看生成的类文件显示了一个主要版本:49 而不是我所期望的 50。Eclipse Indigo 正确创建了主版本为 50 的 1.6 兼容类文件。我在 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 得到相同的输出。