Where to place module-info.java using Java 9?

I have an OSGI application and I have about 30 packages (jar files). Today I decided to see how it works / if it works with Java 9.

So, I started my application and got

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.apache.felix.framework.util.SecureAction (file:/home/.../jar/org.apache.felix.framework-5.4.0.jar) to method java.net.URLClassLoader.addURL(java.net.URL) WARNING: Please consider reporting this to the maintainers of org.apache.felix.framework.util.SecureAction 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 

After some reading, I added a command line option

 --add-exports java.base/java.net=org.apache.felix.framework 

and created a module-info.java file with the following contents:

 module org.apache.felix.framework { } 

And I have two questions. Where should I place this module-info.java to make jvm read it? How to associate this information with the module org.apache.felix.framework-5.4.0.jar file / bundle / jar?

If I do everything wrong, show me the right direction to fix this problem.

+5
source share
3 answers

General tips:

To answer your specific questions:

A module declaration ( module-info.java ) must go into the original root directory (e.g. src/main/java ). Then it should be among the list of files to compile, so it will turn into a module descriptor ( module-info.class ). The final step is to include it in the list of class files that are packaged in the JAR. (Having a module descriptor in a JAR turns it into a modular JAR. If it is placed on the module path, JPMS turns it into a module.)

If you still do not want to create modules and prefer your code to run in an unnamed module, you can allow it to access the internal APIs using the ALL-UNNAMED - in case of f, your warning you need to open this package for thought:

 --add-opens java.base/java.net=ALL-UNNAMED 

A better solution would be to stop using internal APIs. 😉 Since this is not your code, but your dependency, you should look for or open a problem with Apache Felix that asks to remove access to the internal API.

+4
source

Well, Jigsaw quick start is a great way to get started if you are starting a project with Java-9.

Where should I place this module-info.java to make jvm read it?

To display your mode name from the module org.apache.felix.framework { } declaration, you must place the module-info.java file in the project directory at:

 src/org.apache.felix.framework/module-info.java 

How to associate this information with the module org.apache.felix.framework-5.4.0.jar file / bundle / jar?

In addition to compiling the modules and packaging them (bind the module-info.class in the top-level directory of the modular jar), you can use the javac and jar commands.

Examples of commands for the above will be something like this:

 $ javac --module-path mods -d mods/org.apache.felix.framework \ src/org.apache.felix.framework/module-info.java src/org.apache.felix.framework/com/apache/felix/framework/Application.java $ jar --create --file=mlib/org.apache.felix.framework.jar \ --main-class=com.apache.felix.framework.Application -C mods/com.apache.felix.framework 

And then you can execute the module using:

 $ java -p mlib -m com.apache.felix.framework 

In addition to the State Module system and Quick-Start documents, to migrate existing projects to Java-9, I suggest you follow the JDK9 Migration Guide , which outlines the clear step required to adapt to Java9.

+3
source

You should send an Apache Felix error, as this is the code in org.apache.felix.framework-5.4.0.jar that makes illegal access and needs to be fixed. I did a quick test with the newer version 5.6.8 and with --illegal-access=debug , and it looks like Felix has a few questions that need attention. Yes, you can temporarily eliminate the warning above by opening the java.net package for "ALL-UNNAMED", but this is just one or more problems.

+2
source

Source: https://habr.com/ru/post/1271546/


All Articles