How to resolve module java.base does not 'opens java.lang' to unnamed module?
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][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][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 ...
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] ------------------------------------------------------------------------[INFO] BUILD FAILURE[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][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][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:
@EnableEurekaServer@SpringBootApplicationpublic class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}
The parent module’s dependency:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent>
<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>
<properties> <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> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
This module’s dependency:
<parent> <groupId>cd826dong.cloud</groupId> <artifactId>springcloud-book-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../parent</relativePath></parent>
<artifactId>service-discovery</artifactId><packaging>jar</packaging><name>SpringCloud Demo Projects(HelloCloud) -- Eureka Server</name>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency></dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>
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][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][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 -versionjava version "1.8.0_121"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:
mvn install
Then re-run the module:
mvn spring-boot:run
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 --versionopenjdk 16.0.2 2021-07-20OpenJDK 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 UP2022-01-13 17:27:30.766 INFO 10418 --- [ Thread-12] e.s.EurekaServerInitializerConfiguration : Started Eureka Server2022-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 82602022-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!