java - 从安装的 java 8 JDK 编译 java 6

标签 java linux java-8 java-6 compiler-version

大学要求我使用 Java 6 编译我的源代码。我目前在我的 PC (Linux) 上安装了 Java 8 JDK。我如何在 Java 6 中编译?

附注我了解我将无法使用任何 Java 7 或 8 功能。

p.p.s.我意识到 Java 6 源代码可以使用 Java 8 JDK 编译得很好,但是我发现我在家里编译时不小心在我的程序中使用了 Java 6 后的特性。当我尝试在 uni 的实验室计算机上编译源代码时,我遇到了一堆错误。例如无法将对象转换为基元。

最佳答案

解决该问题的方法是使用更高版本的 SDK 进行编译并使用 cross compilation options编译时。

Cross-Compilation Options

By default, classes are compiled against the bootstrap and extension classes of the platform that javac shipped with. But javac also supports cross-compiling, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use the -bootclasspath and -extdirs options when cross-compiling.

-target version

Generates class files that target a specified release of the virtual machine. Class files will run on the specified target and on later releases, but not on earlier releases of the JVM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), 1.7 (also 7), and 1.8 (also 8).

The default for the -target option depends on the value of the -source option:

  • If the -source option is not specified, then the value of the -target option is 1.8
  • If the -source option is 1.2, then the value of the -target option is 1.4
  • If the -source option is 1.3, then the value of the -target option is 1.4
  • If the -source option is 1.5, then the value of the -target option is 1.8
  • If the -source option is 1.6, then the value of the -target is option 1.8
  • If the -source option is 1.7, then the value of the -target is option 1.8
  • For all other values of the -source option, the value of the -target option is the value of the -source option.

-bootclasspath bootclasspath

Cross-compiles against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives.

要完全正确地使用这些选项(即同时使用 -bootclasspath 选项)需要目标版本的 JRE(而非 JDK)的 rt.jar

关于java - 从安装的 java 8 JDK 编译 java 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32578086/

相关文章:

java - Spring-LDAP - 密码比较如何更新失败的密码尝试

c++ - 为什么我不能从主函数返回更大的值?

python - 启动我的恶魔时 Systemctl 阻塞

Java如何使用绘制圆弧并连接到另一个圆弧

java - 向命令行 Spring Boot 应用程序添加对 Spark 的依赖可防止 JVM 退出

JDK 1.6以上没有main方法的Java程序

Linux 外壳 : How to give parameter to alias of find command?

Java数据结构: Map with duplicate keys and sorting by values

java - 在 Java 8 中反转比较器

java - Java8中如何进行分组和缩减