0

I am trying to create Dynamic classes in Java 17 spring boot application using Javasssist library and this works only when we add VM option as --add-opens java.base/java.lang=ALL-UNNAMED in the spring boot Run Config. If this VM option is not added we get error

unable to make protected final java.lang.class java.lang.classloader.defineclass security protection domain throws java.langClassformaterror accessible: module java.base does not openns java.lang to unnamed module

Now when we want to deploy our application, we need some alternative to --add-opens java.base/java.lang=ALL-UNNAMED added in the code say pom.xml or some other file as we don't want to use VM options. But none of the options added in pom.xml seem to work. We always get the same error.

I have tried to add many POM build plugins like

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
     <argLine>
     --add-opens java.base/java.lang=ALL-UNNAMED
     </argLine>
  </configuration>
</plugin>

or

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <configuration>
                <jvmArguments>--add-opens java.base/java.lang=ALL-UNNAMED</jvmArguments>
            </configuration>
        </plugin>
    </plugins>
</build>

but nothing worked. How can I solve this error codewise as an alternative to VM option?

7
  • @FLUXparticle can you please help Commented Aug 5, 2024 at 10:35
  • There is no alternative. It's the JVM option, or, find an alternative to whatever needs to call defineClass on another classloader. That might be possible, but, you haven't included any details whatsoever about why or what code needs to do that. Commented Aug 5, 2024 at 10:48
  • @rzwitserloot thanks for your reply. My task is to call a method createDynamicEntityClass of my Service which uses javassist to create dynamic classes. When we do dynamic class ctClass.toClass() we face error as mentioned above. Only adding VM option helps in spring boot run config. Rather than the config VM option can you please tell code wise how we can fix this error? What changes we can add in code so restriction of java 17 reflection will be avoided and this error is solved when we run our application without VM? Commented Aug 5, 2024 at 11:48
  • @Naman can you help pls Commented Aug 5, 2024 at 12:29
  • 2
    You're clearly not getting the message, sushmita. Not possible. At best you can modify the code that does this, but it sounds like it isn't your code. Commented Aug 5, 2024 at 12:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.