1. Overview
1.概述
In this tutorial, we’ll explore the –source and –target options provided by Java. Additionally, we’ll learn how these options work in Java 8 and how they’ve evolved from Java 9 onwards.
在本教程中,我们将探讨Java提供的-source和-target选项。此外,我们将学习这些选项在Java 8中是如何工作的,以及它们从Java 9开始是如何演变的。
2. Backward Compatibility With Older Java Versions
2.向后兼容旧的Java版本</b
As Java releases and updates are frequent, an application may not be able to migrate to newer versions every time. It’s sometimes necessary for applications to ensure their code is backward compatible with an older version of Java. The target and source options in javac make it easy to accomplish this.
由于Java的发布和更新非常频繁,一个应用程序可能无法每次都迁移到较新的版本。有时,应用程序有必要确保其代码向后兼容旧版本的Java。javac中的target和source选项可以轻松实现这一目的。
To understand this in detail, first, let’s create a sample class and use the List.of() method added in Java 9, but not present in Java 8 :
为了详细了解这一点,首先,让我们创建一个示例类,并使用Java 9中添加的List.of()方法,但在Java 8中不存在。
public class TestForSourceAndTarget {
public static void main(String[] args) {
System.out.println(List.of("Hello", "Baeldung"));
}
}
Let’s assume we’re using Java 9 for compiling the code and want compatibility with Java 8.
We can achieve this using -source and -target:
让我们假设我们使用Java 9来编译代码,并希望与Java 8兼容。
我们可以使用-source和-target来实现。
/jdk9path/bin/javac TestForSourceAndTarget.java -source 8 -target 8
Now, we get a warning on the compilation, but the compilation is successful:
现在,我们在编译时得到一个警告,但编译是成功的。
warning: [options] bootstrap class path not set in conjunction with -source 8
1 warning
Let’s run the code with Java 8, and we can see the error:
让我们用Java 8运行这段代码,我们可以看到错误。
$ /jdk8path/bin/java TestForSourceAndTarget
Exception in thread "main" java.lang.NoSuchMethodError: ↩
java.util.List.of(Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/List;
at com.baeldung.TestForSourceAndTarget.main(TestForSourceAndTarget.java:7)
In Java 8, List.of() is not present. Ideally, Java should have thrown this error at compilation time. However, during compilation, we only got a warning.
在Java 8中,List.of()并不存在。理想情况下,Java应该在编译时抛出这个错误。然而,在编译过程中,我们只得到一个警告。
Let’s take a look at that warning we got during the compilation. javac informed us that the bootstrap classes are not in conjunction with –source 8. As it turns out, we have to provide the bootstrap class file path so that javac can pick the correct file for cross-compilation. In our case, we wanted the compatibility for Java 8, but the Java 9 bootstrap class got picked by default.
让我们来看看我们在编译过程中得到的那个警告。javac通知我们,bootstrap类没有与-source8结合。事实证明,我们必须提供引导类的文件路径,以便javac能够挑选正确的文件进行交叉编译。在我们的案例中,我们希望能够兼容Java 8,但是Java 9引导类被默认选中。
For this to work, we must use –Xbootclasspath to point to the path of the Java version for which cross-compilation is desired:
要做到这一点,我们必须使用-Xbootclasspath来指向需要交叉编译的Java版本的路径。
/jdk9path/bin/javac TestForSourceAndTarget.java -source 8 -target 8 -Xbootclasspath ${jdk8path}/jre/lib/rt.jar
Now, let’s compile it, and we can see the error at compile time:
现在,让我们来编译它,我们可以看到在编译时出现的错误。
TestForSourceAndTarget.java:7: error: cannot find symbol
System.out.println(List.of("Hello", "Baeldung"));
^
symbol: method of(String,
String)
location: interface List
1 error
3. Source Option
3.源选项
The –source option specifies the Java source code version accepted by the compiler:
–source选项指定了编译器所接受的Java源代码版本:。
/jdk9path/bin/javac TestForSourceAndTarget.java -source 8 -target 8
Without the -source option, the compiler will compile with source code based on the Java version being used.
如果没有-source选项,编译器将根据正在使用的Java版本用源代码进行编译。
The -source value 8 also means that we cannot use any API specific to Java 9. In order to use any of the APIs introduced in Java 9, such as List.of(), we must set the value as 9 for the source option.
-source值8也意味着我们不能使用Java 9特有的任何API。 为了使用Java 9中引入的任何API,例如List.of(),我们必须将source选项的值设置为9。
4. Target Option
4.目标选项
The target option specifies the Java version of the class files to produce. The target release must be equal to or higher than the source option:
目标选项指定了要生成的类文件的Java版本。目标版本必须等于或高于源选项:。
/jdk9path/bin/javac TestForSourceAndTarget.java -source 8 -target 8
Here, the –target value 8 means this will generate a class file that requires Java 8 or above to run.
We’ll get an error if we run the above class file in Java 7.
这里,–target值8意味着这将生成一个需要Java 8或以上版本才能运行的类文件。
如果我们在Java 7中运行上面的类文件,我们会得到一个错误。
5. Source and Target in Java 8 and Earlier
5.Java 8及更早版本中的源和目标
As we can see from our example, to have cross-compilation work correctly until Java 8, we need to provide three options, namely –source, -target, and -Xbootclasspath. For example, if we need to build code with Java 9 but it needs to be compatible with Java 8:
从我们的例子中可以看出,为了让交叉编译在Java 8之前正常工作,我们需要提供三个选项,即-source,-target,和Xbootclasspath。例如,如果我们需要用Java 9来构建代码,但它需要与Java 8兼容。
/jdk9path/bin/javac TestForSourceAndTarget.java -source 8 -target 8 -Xbootclasspath ${jdk8path}/jre/lib/rt.jar
From JDK 8, the use of a source or target of 1.5 or earlier is deprecated, and in JDK 9, support for a source or target of 1.5 or earlier is completely removed.
从 JDK 8 开始,使用 1.5 或更早的源码或目标码已被废弃,而在 JDK 9 中,对 1.5 或更早的源码或目标码的支持已完全取消。
6. Source and Target in Java 9 and Later
6.Java 9及以后版本中的源和目标
Even though cross-compilation works fine in Java 8, three command-line options are necessary. When we have three options, it can be difficult to keep them all up to date.
尽管交叉编译在Java 8中运行良好,但三个命令行选项是必要的。当我们有三个选项时,要保持它们都是最新的可能会很困难。。
As part of Java 9, the –release option was introduced to streamline the cross-compilation process. With the –release option, we can accomplish the same cross-compilation as the previous options.
作为 Java 9 的一部分,引入了–release 选项以简化交叉编译过程。通过 –release 选项,我们可以完成与之前选项相同的交叉编译。
Let’s use the –release option to compile our previous sample class:
让我们使用-release选项来编译我们之前的示例类。
/jdk9path/bin/javac TestForSourceAndTarget.java —release 8
TestForSourceAndTarget.java:7: error: cannot find symbol
System.out.println(List.of("Hello", "Baeldung"));
^
symbol: method of(String,String)
location: interface List
1 error
It is evident that only one option -release is required during compile time, and the error indicates that javac has internally assigned the correct values for -source, -target, and -Xbootclasspath.
显然,在编译时只需要一个选项-release,错误表明javac在内部为-source,-target,和Xbootclasspath分配了正确的值。
7. Conclusion
7.结论
In this article, we learned about the –source and –target options of javac and their relation with cross-compilation. Furthermore, we discovered how they are used in Java 8 and beyond. Also, we’ve learned about the -release option introduced in Java 9.
在这篇文章中,我们了解了javac的-source和-target选项以及它们与交叉编译的关系。此外,我们还发现了它们在Java 8及以后的使用情况。另外,我们还了解了Java 9中引入的-release选项。