gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:04:11 24 4
gpt4 key购买 nike

大学要求我使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com