Migrating the Spring Boot application to Java 9: Compatibility
With the advent of Java 9, there is a lot of discussion about how to migrate applications to use modular systems. Unfortunately, most of the articles focus on the simple Hello World program. Or worse, for Spring applications, the sample application uses traditional practices-such as using XML as an example.
The purpose of this article is to correct this by providing a gradual migration guide to the Spring Boot program, the sample program used in this article is the Spring Pet clinic.
There are two main steps to using Java 9: First, to be compatible, and then to use the complete modular system. The purpose of this article is to achieve 1th, and subsequent articles will consider the latter point.
Conflicting Java versions
Once the JDK9 is available on the target machine, first resolve the conflict in the POM and change the value of java.version from 8 to 9:
9
Now, you can do mvn clean compilation.
Conflicting Java versions
The first error found is this:
[ERROR] Failed to execute goal Org.codehaus.mojo:cobertura-maven-plugin:2.7:clean (default) on project Spring-petclinic:
Execution default of goal Org.codehaus.mojo:cobertura-maven-plugin:2.7:clean failed:
Plugin org.codehaus.mojo:cobertura-maven-plugin:2.7 or one of its dependencies could not be resolved:
Could not find artifact com.sun:tools:jar:0 at
Specified path/library/java/javavirtualmachines/jdk-9.jdk/contents/home/. /lib/tools.jar, [Help 1]
Cobertura is a free Java Code Coverage Reporting tool.
-https://github.com/cobertura/cobertura
It requires access to the Tools.jar, and this part is JDK8 (or earlier). One of the changes in Java 9 is to remove the library. Therefore, it cannot be compiled. This is already a documented issue.
The last commit of the Cobertura library was a year ago, just commented on the Cobertura Maven plugin. Think of it or let Jacoco replace Cobertura.
WRO4J Error
The next error is:
[ERROR] Failed to execute goal ro.isdc.wro4j:wro4j-maven-plugin:1.8.0:run (default) on project Spring-petclinic:
Execution default of goal Ro.isdc.wro4j:wro4j-maven-plugin:1.8.0:run failed:
An API incompatibility is encountered while executing Ro.isdc.wro4j:wro4j-maven-plugin:1.8.0:run:
Java.lang.ExceptionInInitializerError:null
Wro4j is a free, open source Java project that can effectively reduce the load time of project pages. It organizes (JS & CSS) static resources, merges or minimizes these resources at runtime (using simple filters) or compilers (using Maven plugins).
There are also a number of useful features that can be used when working with Web resources.
-https://github.com/wro4j/wro4j
This question is quoted from Github issue (https://github.com/wro4j/wro4j/issues/1039). The changes have been submitted and merged, but this issue is still unresolved because Java 9 compatibility should be part of version 2.0.
Now let's add a note to wro4j.
Compilation failed
The compiled project now displays the following error message:
/users/i303869/projects/private/spring-petclinic/src/main/java/org/springframework/samples/petclinic/vet/ Vet.java
Error: (+) Java:package javax.xml.bind.annotation is not visible
(Package Javax.xml.bind.annotation was declared in Module Java.xml.bind, which was not in the module graph)
/users/i303869/projects/private/spring-petclinic/src/main/java/org/springframework/samples/petclinic/vet/ Vets.java
Error: (+) Java:package javax.xml.bind.annotation is not visible
(Package Javax.xml.bind.annotation was declared in Module Java.xml.bind, which was not in the module graph)
Error: (java:package) javax.xml.bind.annotation is not visible
(Package Javax.xml.bind.annotation was declared in Module Java.xml.bind, which was not in the module graph)
This means that the code under the Classpath cannot access the module by default. This is a problem that needs to be added manually with the Javac compiler directive in Java 9 plus the option-add-modules. In Maven, you can set it with Maven-compiler-plugin:
Maven-compiler-plugin
3.7.0
--add-modules
Java.xml.bind
Now the project can be compiled.
Test failure
The next step is to use the MVN test to fail the unit test process.
The cause is the same, but more difficult to find. It needs to check for those foolproof reports. Some contain exceptions in the following line:
caused by:java.lang.ClassNotFoundException:javax.xml.bind.JAXBException
Once again, the test code cannot access the module. However, this time, you need to configure the Maven-surefire-plugin plugin:
Maven-surefire-plugin
2.20.1
--add-modules Java.xml.bind
This will make the test work effective.
Packet failure
If someone thinks this is the end of the road, think again. A fairly mysterious error can also occur during the packaging phase:
[ERROR] Failed to execute goal Org.apache.maven.plugins:maven-jar-plugin:2.6:jar (Default-jar) on project Spring-petclinic:
Execution Default-jar of goal Org.apache.maven.plugins:maven-jar-plugin:2.6:jar failed:
An API incompatibility is encountered while executing Org.apache.maven.plugins:maven-jar-plugin:2.6:jar:
Java.lang.ExceptionInInitializerError:null
...
caused By:java.lang.arrayindexoutofboundsexception:1
At Org.codehaus.plexus.archiver.zip.AbstractZipArchiver. (abstractziparchiver.java:116)
This is harder to find: it requires Google search to find a solution. This is blamed on plexus-archiver. Maven-jar-plugin conflicts with the latest version, and this article is written with the Java 9 compatible version of the archiver version, which resolves this issue:
Maven-jar-plugin
3.0.2
Spring Boot Plugin Error
At this point, the project can be compiled, tested and packaged in a normal way. The next step is to run the application using the Spring Boot Maven plugin, the MVN spring-boot:run. But again it shows that the run failed ... :
[INFO]---spring-boot-maven-plugin:1.5.1.release:run (default-cli) @ spring-petclinic---
[INFO] Attaching agents: []
Exception in thread "main" java.lang.ClassCastException:
Java.base/jdk.internal.loader.classloaders$appclassloader cannot is cast to Java.base/java.net.urlclassloader
At O.s.b.devtools.restart.defaultrestartinitializer.geturls (defaultrestartinitializer.java:93)
At O.s.b.devtools.restart.defaultrestartinitializer.getinitialurls (defaultrestartinitializer.java:56)
At O.s.b.devtools.restart.restarter. (restarter.java:140)
At O.s.b.devtools.restart.restarter.initialize (restarter.java:546)
At O.s.b.devtools.restart.restartapplicationlistener.onapplicationstartingevent (RestartApplicationListener.java : 67)
At O.s.b.devtools.restart.restartapplicationlistener.onapplicationevent (restartapplicationlistener.java:45)
At O.s.c.event.simpleapplicationeventmulticaster.invokelistener (simpleapplicationeventmulticaster.java:167)
At O.s.c.event.simpleapplicationeventmulticaster.multicastevent (simpleapplicationeventmulticaster.java:139)
At O.s.c.event.simpleapplicationeventmulticaster.multicastevent (simpleapplicationeventmulticaster.java:122)
At O.s.b.context.event.eventpublishingrunlistener.starting (eventpublishingrunlistener.java:68)
At O.s.b.springapplicationrunlisteners.starting (springapplicationrunlisteners.java:48)
At O.s.b.springapplication.run (springapplication.java:303)
At O.s.b.springapplication.run (springapplication.java:1162)
At O.s.b.springapplication.run (springapplication.java:1151)
At Org.springframework.samples.petclinic.PetClinicApplication.main (petclinicapplication.java:32)
This is because the Spring Boot v1.5 Developer Tool is incompatible with Java 9 and can be found in the documented issue.
Fortunately, this bug has been fixed in Spring Boot 2.0.0.M5. Unfortunately, when writing this article, the specific version is still unavailable. Now, remove the developer tool and try running again. Failed again, but this time it shows a familiar exception:
caused by:java.lang.ClassNotFoundException:javax.xml.bind.JAXBException
Add the necessary parameters to the Spring-boot-maven-plugin:
Org.springframework.boot
Spring-boot-maven-plugin
--add-modules Java.xml.bind
...
The app is finally up and ready to be accessed!
Conclusion
Running an important legacy project in JDK 9 takes a bit of effort. To make matters worse, some important features must be discarded: Code coverage and Web performance improvements. In turn, the only trivial benefit is the stirng memory space improvement.
Chengdu Java Training which is good, of course, Chengdu training, Chengdu Dahne is a professional program training institutions, focus on network marketing courses, Chengdu Web Front-end training, Chengdu Software testing training, Chengdu PHP training, Chengdu Java training, Chengdu Android training, Chengdu Accounting Practice training, Web front-end development, Chengdu Network Marketing Training, Chengdu IT training, Chengdu Programming training, Chengdu Programmer Training and other IT training, professional Chengdu software training institutions, professional teacher teaching, real project actual combat, 0 first pay, low deposit, name Enterprise employment, Dahne training How, Chengdu Java training Institutions www.cdtedu.com/pxkc/java/
Migrating the Spring boot application to JAVA9: Compatibility