Java11 features and migration guide

What’s new in Java 9, 10 11?

There are 100’s of new features and improvements in these 3 releases. I would try to keep the list short and focus on the ones which are relevant for most.

//Sample Module information file for java.sql module.
module-info.java
module java.sql {
exports java.sql; //packages
exports javax.sql;
exports javax.transaction.xa;
requires java.logging; //other modules
requires java.xml;
}
var authors = Book.getBook().getAuthors();
var firstAuthor = authors.stream().findFirst();
var length = firstAuthor.map(String::length).orElse(0);
List<String> namesList = List.of(“name1”, “name2”);
Set<String> namesSet = Set.of(“name1”, “name2”);
Map<String, String> namesMap = Map.ofEntries(Map.entry(“1”,“name1”),
Map.entry(“2”, “name2”));
HttpClient httpClient = HttpClient.newBuilder()
.version(Version.HTTP_2)
.build();
HttpRequest req = HttpRequest.newBuilder(
URI.create("https://google.com"))
.GET()
.build();
CompletableFuture<HttpResponse<String>> resFuture = httpClient.sendAsync(req, BodyHandlers.ofString());resFuture.thenAccept(res -> System.out.println(res.version()));
resFuture.join();
java -XX:+UseAppCDS -Xshare:dump 
-XX:SharedClassListFile=classes.lst
-XX:SharedArchiveFile=app-cds.jsa
--class-path app.jar
ProcessHandle.current().info()

Tips for Upgrading to Java 11

Upgrade Process: Every time we upgrade a major version, our approach has been the same. i.e. It goes through 2 phases. During the phase 1, we upgrade the runtime, execute the full test suite, address issues, then upgrade non-production instances, monitor them for a release, then upgrade production instances with new runtime. In phase 2, do all this again, but this time, use the new compiler as well. This may sound a bit slow, but given our size, this approach has proven to be least disruptive.

jdeps --jdk-internals -R --class-path ‘libs/*’ $project
Mockito — 3.3.3, powermock — 2.0.7, JMock — 1.49

Compile with Java11:

Internal API usages: During compilation, if you see any failures related to any internal API usages such as “sun.*” or “com.sun.*”, clean those up. i.e. replace those with recommended alternatives.

//if(IPAddressUtil.isIPv4LiteralAddress(col) || IPAddressUtil.isIPv6LiteralAddress(col)) { 
if (InetAddressValidator.getInstance().isValid(col))
-- add-exports to export a package, which makes its public types and members accessible ( javac and java)
Ex: javac --add-exports java.base/sun.security.x509=ALL-UNNAMED Main.java
java --add-exports java.base/sun.security.x509=ALL-UNNAMED Main
java.transaction; 
java.xml.bind;
java.xml.ws;
java.xml.ws.annotation;
java.corba;
java.activation;
-- add-modules adds the listed modules and their transitive dependencies to the module graph
Ex: javac --add-modules java.xml.bind Main.java
java --add-modules java.xml.bind Main
--If you want to add all EE modules
javac --add-modules java.se.ee Main.java

Modularizing your Application

It is not mandatory to modularize your application for running on Java 11. But it may be worth considering, depending on the cost/benefit analysis. Java 11 supports both the classpath and module path. So, all the dependencies can still be mentioned in the classpath. Java internally groups those JARs on the classpath as one module, called the “Unnamed module”. If you are interested in modularizing your app, you should follow the approach similar to the JDK.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store