javac 源和目标选项 [英] javac source and target options

查看:24
本文介绍了javac 源和目标选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了 哪些 JDK 发行版可以运行`javac -source 1.6 -target 1.5`?.我了解源和目标的各个选项.我不明白为什么源版本高于目标版本.为旧目标编译代码是有意义的.但是在那种情况下,为什么我们不使用我们希望能够运行的最旧目标的 -source

I have seen the compile options like discussed in Which JDK's distributions can run `javac -source 1.6 -target 1.5`?. I understand the individual options for source and target. I don't understand why source version is higher that the target version. Compiling the code for older targets makes sense. But in that case, why dont we just use -source of the oldest target we want to be able to run on

推荐答案

  • source:您的源代码需要编译的版本.
  • 目标:您要支持的最旧 JRE 版本.
  • 请务必同时设置 bootclasspath 以确保您的程序可以在较旧的 VM 上运行.

    Be sure to also set bootclasspath to ensure your program will work on older VMs.

    来自 javac 文档:

    以下示例使用 javac 编译将在 1.6 虚拟机上运行的代码.

    Cross-Compilation Example

    The following example uses javac to compile code that will run on a 1.6 VM.

    C:>javac -source 1.6 -target 1.6 -bootclasspath C:jdk1.6.0lib
    t.jar -extdirs "" OldCode.java
    

    -source 1.6 选项指定使用 Java 编程语言的 1.6(或 6)版来编译 OldCode.java.-target 1.6 选项确保生成的类文件将与 1.6 虚拟机兼容.注意,在大多数情况下,-target 选项的值就是-source 选项的值;在本例中,您可以省略 -target 选项.

    The -source 1.6 option specifies that version 1.6 (or 6) of the Java programming language be used to compile OldCode.java. The option -target 1.6 option ensures that the generated class files will be compatible with 1.6 VMs. Note that in most cases, the value of the -target option is the value of the -source option; in this example, you can omit the -target option.

    您必须指定 -bootclasspath 选项以指定引导程序类(rt.jar 库)的正确版本.如果没有,编译器会生成以下警告:

    You must specify the -bootclasspath option to specify the correct version of the bootstrap classes (the rt.jar library). If not, the compiler generates the following warning:

    C:>javac -source 1.6 OldCode.java
    warning: [options] bootstrap class path not set in conjunction with -source 1.6
    

    如果您没有指定正确版本的引导类,编译器将使用旧的语言规则(在本例中,它将使用 Java 编程语言的 1.6 版)结合新的引导类,这可能导致无法在旧平台(在本例中为 Java SE 6)上运行的类文件,因为可能会包含对不存在的方法的引用.

    If you do not specify the correct version of bootstrap classes, the compiler will use the old language rules (in this example, it will use version 1.6 of the Java programming language) combined with the new bootstrap classes, which can result in class files that do not work on the older platform (in this case, Java SE 6) because reference to non-existent methods can get included.

    这篇关于javac 源和目标选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆