Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

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.

2. Backward Compatibility With Older Java Versions

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.

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 :

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:

/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:

$ /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.

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.

For this to work, we must use –Xbootclasspath to point to the path of the Java version for which cross-compilation is desired:

/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

The –source option specifies the Java source code version accepted by the compiler:

/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.

In our example, If -source 8 is not provided, the compiler will compile source code in accordance with Java 9 specifications.

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.

4. Target Option

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:

/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.

5. Source and Target in Java 8 and Earlier

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:

/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.

6. Source and Target in Java 9 and Later

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.

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.

Let's use the –release option to compile our previous sample class:

/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.

7. Conclusion

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.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!