This short article shows how to set up and use Java 18 in your Spring Boot applications.
Java 18 overview
Java 18 is available since March 2022. You can download it from the following link: https://jdk.java.net/18/
Here is a list of the highlights for this server version:
- JEP 400: UTF-8 by Default sets UTF-8 as the default character set for standard Java APIs. With this change, APIs that depend on the default character set will operate consistently across all implementations, operating systems, local versions, and configurations.
- JEP 408: Simple Web Server, a command-line tool and API for starting a minimal Web server that distributes only static files. This tool is useful for prototyping, writing ad hoc code and testing, particularly in educational contexts.
- JEP 416: Reimplement Core Reflection with Method Handles reimplements java.lang.reflect.Method, constructor and field on java.lang.invoke method handles. By making method handles the core reflection mechanism, you reduce the maintenance and development costs of the java.lang.reflect and java.lang.invoke APIs.
- JEP 418: Internet-Address Resolution SPI defines a Service-Provider Interface (SPI) for hostname and address resolution, so that java.net.InetAddress can use resolvers other than the one built into the platform.
- JEP 413: JEP Code Snippets in Java API Documentation introduces the @snippet tag for the standard JavaDoc Doclet to simplify the inclusion of sample source code in API documentation.
- JEP 417: Vector API (Third Incubator) provides developers with an API to reliably take advantage of CPU architectures that offer scalable vector extensions. This will provide superior performance over equivalent calculations in non-extended processors.
- JEP 419: Foreign Function and Memory API (Second Incubator) allows Java programs to interact with code and data outside the Java runtime. By efficiently calling foreign functions (i.e., code outside of the JVM) and safely accessing foreign memory (e.g., memory not managed by the JVM), the API allows Java programs to call native libraries and process native data without the fragility and pitfalls of JNI.
- JEP 420: Pattern Matching for Switch (Second Preview) enhances the Java programming language with pattern matching for switch expressions and instructions and pattern language extensions. The extension of pattern matching to the switch allows an expression to be tested against several patterns, each with a specific action, so that complex data-based queries can be expressed briefly and safely.
Configuring Spring Boot to use Java 18
You can use the Spring Boot initializr to bootstrap the Java 18 application. As you can see, it’s already included in the initializr options:
If you are using Maven to build your projects, simply include the property “java.version” in your pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>18</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--build section hee --> </project>
On the other hand, if you use Gradle to build your project, set in your build.gradle the sourceCompatibility property to 18:
plugins { id 'org.springframework.boot' version '2.6.5' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '18' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
That’s all! Have fun with Java 18 and Spring Boot!
Found the article helpful? if so please follow us on Socials