1. Purpose
In this post, I will demonstrate how to resolve the following error when running spring cloud eureka server:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run ( default-cli ) on project service-discovery: An exception occurred while running. null: InvocationTargetException: Cannot load configuration class: org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration: ExceptionInInitializerError: Unable to load cache item: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass ( java.lang.String,byte[],int,int,java.security.ProtectionDomain ) throws java.lang.ClassFormatError accessible: module java.base does not " opens java.lang " to unnamed module @505e2a79 - > [Help 1]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
The key problem is below:
java.lang.ClassFormatError accessible: module java.base does not " opens java.lang " to unnamed module ...
What is java.lang.ClassFormatError?
In Java, java.lang.ClassFormatError
is a runtime error that occurs when the Java Virtual Machine (JVM) attempts to load a class file that is not correctly formatted or violates the Java class file specification.
It is part of the java.lang package and extends the java.lang.LinkageError
. This error typically arises from issues such as:
Corrupted Class Files: The .class file might have been altered, damaged, or truncated.
Incompatible or Invalid Bytecode: The class file may not conform to the version or structure expected by the JVM.
Third-Party Tools or Libraries: Generated or modified class files by tools or libraries that do not adhere strictly to the class file format.
Class File Mismatch: An incorrect combination of compiled and runtime environments.
Example Usage
Although ClassFormatError is generally thrown by the JVM, you can catch it in your application to handle the error gracefully:
Class . forName ( " SomeInvalidClass " ) ;
} catch ( ClassFormatError e ) {
System . out . println ( " Class format is invalid: " + e . getMessage ()) ;
However, encountering this error typically indicates a problem with the build or deployment process, so fixing the underlying issue is the best course of action.
2. The solution
The detailed error
I want to start an spring cloud eureka server in IntelliJ IDEA, but I got this error:
/Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java " -Dmaven.multiModuleProjectDirectory=/Users/bswen/JavaProjects/SpringCloudLearning202201/springcloud-demo/chapter03 -- hello-cloud/service-discovery " " -Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3 " " -Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf " " -Dmaven.ext.class.path=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven-event-listener.jar " -Dfile.encoding=UTF-8 -classpath " /Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.6.0.jar " org.codehaus.classworlds.Launcher -Didea.version2019.3.3 org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run
[INFO] Scanning for projects...
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass ( java.lang.String,byte[],int,int,java.security.ProtectionDomain ) throws java.lang.ClassFormatError accessible: module java.base does not " opens java.lang " to unnamed module @505e2a79
at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:357)
at com.cd826dong.clouddemo.Application.main (Application.java:32)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method )
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:78)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.springframework.boot.maven.AbstractRunMojo $LaunchRunner .run (AbstractRunMojo.java:527)
at java.lang.Thread.run (Thread.java:831)
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run ( default-cli ) on project service-discovery: An exception occurred while running. null: InvocationTargetException: Cannot load configuration class: org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration: ExceptionInInitializerError: Unable to load cache item: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass ( java.lang.String,byte[],int,int,java.security.ProtectionDomain ) throws java.lang.ClassFormatError accessible: module java.base does not " opens java.lang " to unnamed module @505e2a79 - > [Help 1]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
The module’s dependency
The source code
The Eureka Server’s source code:
public class Application {
public static void main ( String [] args ) {
new SpringApplicationBuilder ( Application . class ) . web ( true ) . run ( args ) ;
The parent module’s dependency:
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-parent </ artifactId >
< version > 1.5.2.RELEASE </ version >
< groupId > cd826dong.cloud </ groupId >
< artifactId > springcloud-book-parent </ artifactId >
< version > 0.0.1-SNAPSHOT </ version >
< packaging > pom </ packaging >
< name > SpringCloud Demo Projects(HelloCloud) -- Parent Pom </ name >
< project.build.sourceEncoding > UTF-8 </ project.build.sourceEncoding >
< java.version > 1.8 </ java.version >
< maven.compiler.source > ${java.version} </ maven.compiler.source >
< maven.compiler.target > ${java.version} </ maven.compiler.target >
< guava.version > 20.0 </ guava.version >
< groupId > org.springframework.cloud </ groupId >
< artifactId > spring-cloud-dependencies </ artifactId >
< version > Dalston.SR4 </ version >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-test </ artifactId >
< groupId > org.apache.maven.plugins </ groupId >
< artifactId > maven-compiler-plugin </ artifactId >
This module’s dependency:
< groupId > cd826dong.cloud </ groupId >
< artifactId > springcloud-book-parent </ artifactId >
< version > 0.0.1-SNAPSHOT </ version >
< relativePath > ../parent </ relativePath >
< artifactId > service-discovery </ artifactId >
< packaging > jar </ packaging >
< name > SpringCloud Demo Projects(HelloCloud) -- Eureka Server </ name >
< groupId > org.springframework.cloud </ groupId >
< artifactId > spring-cloud-starter-eureka-server </ artifactId >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-maven-plugin </ artifactId >
if start from command line, we got this error:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run ( default-cli ) on project springcloud-book-parent: Unable to find a suitable main class, please add a ' mainClass ' property - > [Help 1]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Let’s Debug the error
Check the java version:
➜ chapter03 -- hello-cloud git: ( master ) ✗ java -version
Java(TM ) SE Runtime Environment ( build 1.8.0_121-b13 )
Java HotSpot ( TM ) 64-Bit Server VM (build 25.121-b13, mixed mode )
The solution
Run the following command in parent module:
Then re-run the module:
But double-clicking the run to run the spring-boot plugin in the idea still reports an error:
The reason of the error
Idea use jdk 1.6
to start spring applicaiton, just as the logs show:
/Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java ...
Then I check:
➜ chapter03 -- hello-cloud git:(master) ✗ /Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java --version
openjdk 16.0.2 2021-07-20
OpenJDK Runtime Environment Temurin-16.0.2+7 (build 16.0.2+7)
OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (build 16.0.2+7, mixed mode, sharing)
➜ chapter03 -- hello-cloud git:(master) ✗
It’s jdk1.6
, NOT jdk1.8
, that is the problem.
Change project sdk to 1.8:
Then click ‘spring-boot:run’ to run again:
2022-01-13 17:27:30.757 INFO 10418 --- [ Thread-12] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2022-01-13 17:27:30.766 INFO 10418 --- [ Thread-12] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2022-01-13 17:27:30.828 INFO 10418 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8260 (http)
2022-01-13 17:27:30.829 INFO 10418 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8260
2022-01-13 17:27:30.835 INFO 10418 --- [ main] com.cd826dong.clouddemo.Application : Started Application in 9.384 seconds (JVM running for 13.583)
It works!
How to change default JAVA sdk version for new java projects?
Click File--Other Settings--Structure
for new projects:
then change default sdk for intellij idea:
3. Summary
In this post, I demonstrated how to solve the module java.base does not "opens java.lang" to unnamed module
error. The key point is to make sure that your java version is correct. That’s it, thanks for your reading.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem.
So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by
email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by
starring the repo on GitHub !