Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illegal reflective access operation #23805

Closed
asimonsy opened this issue Feb 14, 2022 · 6 comments
Closed

Illegal reflective access operation #23805

asimonsy opened this issue Feb 14, 2022 · 6 comments
Assignees

Comments

@asimonsy
Copy link

Environment Details

  • GlassFish Version: 6.2.5
  • JDK version: 17.0.1

Problem Description

This is the stack trace I get when I try to run my application compiled in JDK 17:

Exception in thread "main" java.lang.Error: Could not initialize access to ClassLoader.defineClass method.
	at com.sun.ejb.codegen.ClassGenerator.<clinit>(ClassGenerator.java:60)
	at com.sun.ejb.codegen.Generator.generate(Generator.java:140)
	at com.sun.ejb.codegen.EjbClassGeneratorFactory.generate(EjbClassGeneratorFactory.java:116)
	at com.sun.ejb.codegen.EjbClassGeneratorFactory.ensureGenericHome(EjbClassGeneratorFactory.java:59)
	at com.sun.ejb.EJBUtils.loadGeneratedGenericEJBHomeClass(EJBUtils.java:384)
	at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:323)
	at com.sun.ejb.containers.RemoteBusinessObjectFactory.getObjectInstance(RemoteBusinessObjectFactory.java:40)
	at java.naming/javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:342)
	at com.sun.enterprise.naming.impl.SerialContext.getObjectInstance(SerialContext.java:503)
	at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:463)
	at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:414)
	at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
	at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
	at com.sun.ejb.codegen.ClassGenerator.lambda$static$0(ClassGenerator.java:51)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
	at com.sun.ejb.codegen.ClassGenerator.<clinit>(ClassGenerator.java:57)
	... 14 more

If I compile and run the same in JDK 11 there are just some warnings:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.ejb.codegen.ClassGenerator to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.ejb.codegen.ClassGenerator
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
@arjantijms
Copy link
Contributor

Thanks for the report, we'll be sure to look at this.

As always, a reproducer will very likely help us a lot here.

@asimonsy
Copy link
Author

Reproducible with the following example. A remote client is supposed to lookup a bean deployed on GlassFish server:

HelloBean.java

package example;

import jakarta.ejb.Stateless;

@Stateless
public class HelloBean implements HelloBeanRemote {
    @Override
    public String hello() {
        return "Hello";
    }
}

HelloBeanRemote.java

package example;

import jakarta.ejb.Remote;

@Remote
public interface HelloBeanRemote {
    String hello();
}

HelloClient.java

package client;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import example.HelloBeanRemote;

public class HelloClient {
    public static void main(String[] args) {
        try {
            InitialContext ctx = new InitialContext();
            Object EjbObject = ctx.lookup("example.HelloBeanRemote");
            HelloBeanRemote helloBean = (HelloBeanRemote) EjbObject;
            System.out.println(helloBean.hello());
        } catch (NamingException nex) {
            nex.printStackTrace();
        }
    }
}

StackTrace:

Exception in thread "main" java.lang.Error: Could not initialize access to ClassLoader.defineClass method.
	at com.sun.ejb.codegen.ClassGenerator.<clinit>(ClassGenerator.java:67)
	at com.sun.ejb.codegen.Generator.generate(Generator.java:127)
	at com.sun.ejb.codegen.EjbClassGeneratorFactory.generate(EjbClassGeneratorFactory.java:116)
	at com.sun.ejb.codegen.EjbClassGeneratorFactory.ensureGenericHome(EjbClassGeneratorFactory.java:59)
	at com.sun.ejb.EJBUtils.loadGeneratedGenericEJBHomeClass(EJBUtils.java:384)
	at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:323)
	at com.sun.ejb.containers.RemoteBusinessObjectFactory.getObjectInstance(RemoteBusinessObjectFactory.java:40)
	at java.naming/javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:342)
	at com.sun.enterprise.naming.impl.SerialContext.getObjectInstance(SerialContext.java:503)
	at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:463)
	at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:414)
	at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
	at client.HelloClient.main(HelloClient.java:11)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @7a36aefa
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
	at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
	at com.sun.ejb.codegen.ClassGenerator.lambda$static$0(ClassGenerator.java:58)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
	at com.sun.ejb.codegen.ClassGenerator.<clinit>(ClassGenerator.java:64)
	... 12 more

@dmatej dmatej self-assigned this Feb 14, 2022
@dmatej
Copy link
Contributor

dmatej commented Feb 14, 2022

And that's because I was lazy to run all tests on JDK17 again! :)
Thanks for the report, you were fast!

@dmatej dmatej added the bug Something isn't working label Feb 14, 2022
@dmatej dmatej added this to the 6.2.6 milestone Feb 14, 2022
@dmatej
Copy link
Contributor

dmatej commented Feb 16, 2022

Btw how do you call the client?
You should use this JVM option --add-opens=java.base/java.lang=ALL-UNNAMED
I will try to run tests with JDK17 for sure today.

@dmatej dmatej removed this from the 6.2.6 milestone Feb 22, 2022
@dmatej dmatej removed the bug Something isn't working label Feb 22, 2022
@dmatej
Copy link
Contributor

dmatej commented Feb 22, 2022

I tried to rerun all tests with JDK17 and they passed, so I expect that you just really need to add the --add-opens option and it should work.

@dmatej dmatej closed this as completed Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants