Illegal reflective access with createFromXmlResource()

252 views
Skip to first unread message

J J Farrell

unread,
Dec 26, 2021, 5:21:13 PM12/26/21
to OptaPlanner development
I'm new to Java, Optaplanner, and gradle, please excuse any ignorance.

I've inherited a plain java application built with gradle which used the API to set up the (fixed) solver configuration. I wanted to move the config to XML, so now have

        SolverFactory<Project> solverFactory = SolverFactory.createFromXmlResource("blah.xml");
        Solver<Project> solver = solverFactory.buildSolver();


Running this on Windows 10 with

$ java --version
java 11.0.11 2021-04-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)


gives

WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
...
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/blah.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
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


On MacOS with

% java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-39)
OpenJDK 64-Bit Server VM (build 17.0.1+12-39, mixed mode, sharing)


I get

java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
        at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:294) ~[blah.jar:?]
...
        at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35) ~[blah.jar:?]
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:358) ~[blah.jar:?]
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691) ~[blah.jar:?]
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632) ~[blah.jar:?]
        at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:88) ~[blah.jar:?]
...


which I assume is the same thing with illegal accesses disabled.

I've searched for this and not found any references to OptaPlanner, but various similar reports for other projects. The fixes seem to be different in each case and I've not been able to work out a common factor to apply here. They seem to mostly involve changes to JAXB implementation or libraries.

Can anyone advise what I need to do to fix this please (other than enable illegal accesses). I'm puzzled why other OptaPlanner users aren't seeing this as I thought XML configuration was usual - or is it just that the fix is obvious to anyone who has a clue?

Thanks for any advice.

Regards,
                         jjf

Geoffrey De Smet

unread,
Dec 28, 2021, 4:44:05 AM12/28/21
to optapla...@googlegroups.com

1) Are you using a module-info.java file - so are you using JPMS (Jigsaw)?
Probably not, but if you are, you 'll need to "open" the module that contains your domain classes.

2) What do you see if you run "gradle -q dependencies"
Compare it with running it for our hello-world app:
  https://github.com/kiegroup/optaplanner-quickstarts/tree/stable/hello-world
Are you using the same jaxb-* versions etc?

With kind regards,
Geoffrey De Smet

--
You received this message because you are subscribed to the Google Groups "OptaPlanner development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to optaplanner-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/optaplanner-dev/22b1d3ac-8190-4c2d-abf3-2099146fc500n%40googlegroups.com.

J J Farrell

unread,
Dec 29, 2021, 9:34:48 PM12/29/21
to OptaPlanner development
Thanks Geoffrey.

1) I'm not using JPMS.

2) I don't see anything obvious on comparing the dependencies. Both runtime dependencies are the same in what appears to be the relevant area:

+--- org.optaplanner:optaplanner-core:8.14.0.Final
:
|    +--- jakarta.xml.bind:jakarta.xml.bind-api:2.3.3
|    |    \--- jakarta.activation:jakarta.activation-api:1.2.2
|    \--- org.glassfish.jaxb:jaxb-runtime:2.3.3-b02
|         +--- jakarta.xml.bind:jakarta.xml.bind-api:2.3.2 -> 2.3.3 (*)
|         +--- org.glassfish.jaxb:txw2:2.3.3-b02
|         +--- com.sun.istack:istack-commons-runtime:3.0.10
|         \--- com.sun.activation:jakarta.activation:1.2.1

My project also pulls in optaplanner-benchmark and a few other things which don't appear to be at all relevant. It doesn't have any other use of XML.

Any other thoughts on how I can get to the bottom of this?

Many Thanks,
                                   jjf

J J Farrell

unread,
Dec 30, 2021, 6:41:06 PM12/30/21
to OptaPlanner development
After reading around a number of similar issues with reflection, I tried updating the manifest for my jar to make it 'multi-release'. This appears to have fixed the problem - at least I no longer see the warnings on Windows, I don't currently have access to the MacOS OpenJDK 17 environment to confirm that the exception has gone. I don't really understand what's happening here or if this is the best solution, but it will do for now until I can get my knowledge up.

Thanks for the help Geoffrey.

Regards,
                           jjf
Reply all
Reply to author
Forward
0 new messages