Source and target in java

How to specify source and target compatibility in java module?

When working with Java modules, it is important to ensure that the source code is compatible with the target platform. This can be accomplished by specifying the source and target compatibility in the module descriptor, or «module-info.java» file. This file is automatically generated by the Java compiler when creating a module, and it contains important information about the module’s dependencies, exports, and requirements.

Method 1: Specify source and target compatibility in the module descriptor

To specify the source and target compatibility in a Java module, you can use the —module-source-path and —module-version options in the module descriptor. Here is an example of how to do it:

module com.example.mymodule  requires java.base; requires java.sql; exports com.example.mypackage; // specify source and target compatibility // with the --module-version option // and the --module-source-path option // the source and target compatibility is set to 11 // and the module source code is located in the src/main/java directory // of the module opens com.example.mypackage to spring.core; provides com.example.mypackage.MyService with com.example.mypackage.MyServiceImpl; >

In this example, the —module-version option is used to specify the version of the module, and the —module-source-path option is used to specify the location of the module source code.

You can also specify the source and target compatibility using the —release option. Here is an example:

module com.example.mymodule  requires java.base; requires java.sql; exports com.example.mypackage; // specify source and target compatibility // with the --release option // the source and target compatibility is set to 11 // and the module source code is located in the src/main/java directory // of the module opens com.example.mypackage to spring.core; provides com.example.mypackage.MyService with com.example.mypackage.MyServiceImpl; > // specify the release version of the module // with the --release option // the source and target compatibility is set to 11 // and the module source code is located in the src/main/java directory // of the module --release 11

In this example, the —release option is used to specify the source and target compatibility.

That’s it! With these options, you can easily specify the source and target compatibility in your Java modules.

Method 2: Use the -source and -target command line options

To specify the source and target compatibility in a Java module, you can use the -source and -target command-line options. These options specify the version of the Java programming language that the compiler should use to compile your code and the version of the Java Virtual Machine (JVM) that should be used to run your code.

Here is an example command that sets the source and target compatibility to Java 11:

javac -source 11 -target 11 MyModule.java

This command tells the compiler to use Java 11 as the source version and to generate class files that are compatible with Java 11.

Here is another example command that sets the source and target compatibility to Java 8:

javac -source 1.8 -target 1.8 MyModule.java

This command tells the compiler to use Java 8 as the source version and to generate class files that are compatible with Java 8.

You can also set the source and target compatibility in your build script, such as Gradle or Maven, by adding the following configurations:

build> plugins> plugin> groupId>org.apache.maven.pluginsgroupId> artifactId>maven-compiler-pluginartifactId> version>3.8.1version> configuration> source>1.8source> target>1.8target> configuration> plugin> plugins> build>

These configurations tell the build tool to use Java 8 as the source version and to generate class files that are compatible with Java 8.

In summary, you can specify the source and target compatibility in a Java module using the -source and -target command-line options or by configuring your build script.

Method 3: Use the -release command line option

To specify source and target compatibility in Java module using the -release command line option, follow these steps:

  1. Open your terminal or command prompt and navigate to the directory where your Java module is located.
  2. Compile your Java module using the -release option and specify the version you want to use. For example, if you want to use Java 11, you would use the following command:
javac -release 11 module-info.java
  1. If you have multiple source files, you can compile them all at once by specifying the directory containing the source files. For example, if your source files are in a directory called «src», you would use the following command:
javac -d out -release 11 src/*.java
  1. When you run your Java module, you also need to specify the release version. For example, if you compiled your module using Java 11, you would run it with the following command:
java --module-path out -m your.module.name/your.main.Class --release 11

Here is an example of a module-info.java file that specifies the release version:

module your.module.name  requires java.base; requires java.sql; exports com.example.package; >

And here is an example of a Java source file that uses Java 11 features:

package com.example.package; import java.util.List; public class ExampleClass  public static void main(String[] args)  ListString> list = List.of("foo", "bar", "baz"); System.out.println(list); > >

By following these steps and using the -release command line option, you can specify the source and target compatibility for your Java module.

Источник

Source and target in java

Apache Maven Compiler Plugin

  • Apache /
  • Maven /
  • Plugins /
  • Apache Maven Compiler Plugin /
  • Setting the -source and -target of the Java Compiler
  • | Last Published: 2023-02-14
  • Version: 3.11.0

Setting the -source and -target of the Java Compiler

Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target . The Compiler Plugin can also be configured to provide these options during compilation.

For example, if you want to use the Java 8 language features ( -source 1.8 ) and also want the compiled classes to be compatible with JVM 1.8 ( -target 1.8 ), you can either add the two following properties, which are the default property names for the plugin parameters:

or configure the plugin directly:

 [. ] [. ]  org.apache.maven.plugins maven-compiler-plugin 3.11.0 1.8 1.8    [. ] [. ] 

Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler’s boot classpath to match the target JRE, or use the Animal Sniffer Maven Plugin to verify your code doesn’t use unintended APIs, or better yet use the release option supported since JDK 9. In the same way, setting the source option does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch Maven, refer to the Compile Using A Different JDK example.

Источник

How To Use Source and Target Parameter in Java Compiler

If you are a Java programmer, then one of the common question comes into mind is How do I compile for compatibility with older versions of Java?. When you run your Java program with an older version of the Java runtime environment, you may get the following Java exception if you have not compiled with the right version of Java language:

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

In one of my previous example, I have explained about how to check the version number of the Java from the class file. In this tutorial I am going to explain the two parameters, target and source which are used for cross compilation and runtime requirements of your Java application.

If you want to ensure the application has to support the minimum runtim environment, then you have to fully understand how to use the source and target parameter for javac. Java compiler javac have the option to set the target runtime environment supported for your application by using the command line arguments target while compiling the source code.

Javac Source and Target Parameters

Javac Source and Target Example

Source parameter tells the compiler that the source code will be compiled with the specific version. The official documentation says the source parameter provides source compatibility with specified release. If you are compiling the source code with Java 1.6, but if the code is having some specific features in the Java 7, then compilation fails. In that case you have to set the source parameter as the Java 1.7.

The following are the accepted parameter value for the release:

  • 1.3 – The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
  • 1.4 – The compiler accepts code containing assertions, which were introduced in JDK 1.4.
  • 1.5 – The compiler accepts code containing generics and other language features introduced in JDK 5.
  • 5 – Synonym for 1.5.
  • 1.6 – This is the default value if you are compiling with JDK 6. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
  • 6 – Synonym for 1.6.
  • 1.7 – This is the default value. The compiler accepts code with features introduced in Java SE 7.
  • 7 – Synonym for 1.7.

Target parameter tells the compiler that what is the minimum targeted JVM that can run the class files. If you pass the target parameter as 1.5, then the class files compatible to run 1.5 and above version of JVM. But, it can not run below 1.5 JVM.

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

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

Let’s look at the below example command:

% javac -source 1.6 -target 1.5

The above code conveys the message that, the code base will be compiled with the Java 1.6 version, but the targeted minimum environment support is Java 1.5. In other words, converted class files are compatible to with 1.6 JVM.

Ant – Source and Target

How to use the source and target parameter of javac can be used in the apache ant script?. Let’s look at the below code snippet of ant script:

Maven – Source and Target

This section provides the sample code snippet for using the source and target parameters of javac in the maven’s pom.xml file.

 [. ] [. ]  org.apache.maven.plugins maven-compiler-plugin 3.5.1 1.5 1.5    [. ] [. ] 

Removing Support for the older versions

There is a JEP 182 which talks about retiring the old version from the support to reduce the cost to maintain multiple versions.

Oracle wants reduce the maintenance costs of javac , so the new JEP 182 defines a policy for retiring old -source and -target options. From JDK 8, 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 will be completely removed.

From JDK 9 and going forward, javac will use a “one + three back” policy of supported source and target options. Under this policy, javac will still be able to recognize and process class files of all previous JDKs, going back to version 45.3 class files generated by JDK 1.0.2, which first shipped in 1996.

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Источник

Читайте также:  METANIT.COM
Оцените статью