16

currently i follow course video from this youtube:https://www.youtube.com/watch?v=BHoiNd64w0c&list=PLQag1tT77Ben3dupVMgYtoi_PVVQVRdD6&index=13
everything works fine, i run my project by clicking play button in eclipse, a little different by instructor because he used sts tool. But the error happen when trying to right click > run as > maven install
based on this question How to fix "Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass?, im already used the newer spring version. Here's my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>java-login-security</groupId>
    <artifactId>login-system</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>login-system</name>
    <description>login system</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>9.0.70</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

and here more error log:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.7)

2023-01-14 12:19:00.834  INFO 2373 --- [           main] j.l.LoginSystemApplicationTests          : Starting LoginSystemApplicationTests using Java 17.0.4.1 on dna with PID 2373 (started by dna in /media/dna/data/koding/java/login-system)
2023-01-14 12:19:00.835  INFO 2373 --- [           main] j.l.LoginSystemApplicationTests          : No active profile set, falling back to 1 default profile: "default"
2023-01-14 12:19:01.333  INFO 2373 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-01-14 12:19:01.380  INFO 2373 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 41 ms. Found 1 JPA repository interfaces.
2023-01-14 12:19:01.530 ERROR 2373 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.cglib.core.CodeGenerationException: 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 @78dd667e
    at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:598) ~[spring-core-5.3.24.jar:5.3.24]
    at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363) ~[spring-core-5.3.24.jar:5.3.24]
// ...more error log here...

my java version:

dna@dna:/$ java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-Ubuntu-2ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.5+8-Ubuntu-2ubuntu122.04, mixed mode, sharing)

[update] you can check my project here: https://github.com/dhanyn10/java/tree/main/login-system

dhanyn10
  • 704
  • 1
  • 9
  • 26

3 Answers3

25

I had the same issue. It was because my project was written in Java 8 but I tried to start in Java 17. I just changed the configuration for Build&Run in IntelliJ from SDK 17 to the correct Java Version 8 and it worked. Maybe you used another Java version for building the project in IntelliJ.

Here's the configuration screenshot.

configuration screen

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
Kappa97
  • 259
  • 1
  • 2
4

I was still getting getting the same error after changing the project to 1.8 as suggested above , but it worked for me after updating the spring boot version from 2.0.2.RELEASE to 2.7.7 , so

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

became

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
Mohammed Fathi
  • 1,237
  • 1
  • 14
  • 12
0

note: writing this as answer as code sample is involved.

What @P.Sanjay said was right and helpful. This is based on the Sanjay's direction and this blog would be helpful as well https://www.springcloud.io/post/2022-07/inaccessibleobjectexception/#gsc.tab=0

you have to find a way to pass these params to spring boot java instance. As I was starting the server (liberty server) directly I had to modify the binary sh file which starts the server. (this is an example from liberty server). I had to add this server file(which starts/stop/restarts the server) which was sort of using "JVM_OPTIONS_QUOTED" variable. I tried to add to jvm.options file but somehow it dint work (may be was not passing things properly) otherwise suggest you add there.

JVM_OPTIONS_QUOTED="-Djava.awt.headless=true -Djdk.attach.allowAttachSelf=true -Dcom.ibm.tools.attach.enable=yes -Dsun.misc.URLClassPath.disableJarChecking=true  --add-opens jdk.naming.rmi/com.sun.jndi.rmi.registry=ALL-UNNAMED"

which would be later called like this

      java '' "${JVM_OPTIONS_QUOTED}" "${SERVER_NAME}" --pid="${PID}" --package "$@" 

that fixed the issue for me. So I think in your case you have add the corresponding package accordingly.

Further I strongly feel this is local/development fix. For production I feel the dependencies have to updated to latest ones, where this is fixed or not used.

Hemanth Kumar
  • 172
  • 1
  • 6