Do I have to add open statement in module-info.java to open module for reflection
My friend and I are building our first modular app and we are facing some weird behavior. Actually, we posted question on SO (), but without any answer. So I decided to post the question here (SO copy-paste):
I'm building my first modular app with Java 11 and Maven 3.6.1. My IDE is IntellijIDEA 2019.1.3. I added a module 'app' and added module-info.java, but I'm confused because my app is working even I added spring dependencies to the app module and I didn't open my module or some package in the module for reflection.
I added module-info.java with my IDE's feature and it forces me to add requires statements. So far so good. But why it works without opening the module for reflection? Is that some new feature in Java 11 or in my IDE version? Am I doing something wrong?
My module-info.java:
module app {
requires spring.web;
requires spring.webmvc;
requires javax.servlet.api;
requires spring.context;
}
I tried to find the answer on SO and JetBrains, but I failed.
And one more question: How can I examine the module-path with my IDE? How can I see which modules are there and what is on my classpath (right now there should be nothing)?
I am looking at this guy (https://youtu.be/hxsCYxZ1gXU?t=2238) working with spring modules and his IDE requires him to open the module for reflection. And I downloaded his project and deleted 'opens' statements and it still compiles in my IDE.
EDIT: I just realized that my IDE prints `Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED` log. I suppose this is the reason, but I can't find where this arg is coming from. Am I on the right trail?
Please sign in to leave a comment.
It looks like you've already got the answer at https://stackoverflow.com/a/58749017/104891.
Kind of... read the comment on the first post there on SO. It turns out that Tomcat is the problem. Tomcat's ClassLoader converts my modules as unnamed. If you are interested enough, any help is welcomed. I don't have a solution yet.
For the Java runtime to work in a module path mode, one has to start it accordingly (like,
java -mp ... -m my.module/my.Klassinstead ofjava -cp ... my.Klass), otherwise protection won't work. That SO answer is correct, in a class path mode all classes are loaded in a single "unnamed" module (except JRE classes, but still). Do you how Tomcat is started in your case?If it is possible for you to share a simple demo project, probably we'll be able to tell more.
Hi Petr! Thank you for being interested. Actually, just a few hours ago we figured out what was the problem and summarized the problem and solution here: https://stackoverflow.com/questions/58737469/does-java-ee-jakarta-ee-support-the-java-module-system-is-it-possible-to-make
Check out the accepted answer, it is pretty interesting and doesn't have anything with IntelliJ.