Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an OSGI application and I have around 30 bundles (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 command line option

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

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

module org.apache.felix.framework { }

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

If I do everything wrong please, show me right direction for fixing this problem.

like image 568
Pavel_K Avatar asked Sep 05 '17 18:09

Pavel_K


People also ask

Where do I put module-info?

The module declaration ( module-info. java ) needs to go into your source root directory (e.g. src/main/java ). It then has to be among the list of files to compile, so it will get turned into a module descriptor ( module-info. class ).

Where is module-info Java located?

The module descriptor ( module-info. java ) needs to be located in the src/main/java directory. Maven will set up javac to use the appropriate module source path.

How do I use Java 9 modules?

To set up a module, we need to put a special file at the root of our packages named module-info. java. This file is known as the module descriptor and contains all of the data needed to build and use our new module. We start the module declaration with the module keyword, and we follow that with the name of the module.


1 Answers

Generic tips:

  • You should read the State of the Module System to familiarize yourself with the JPMS. Alternatively, have a look at this module system tutorial (disclaimer: I'm the author).
  • As described in the answer to your other question, there is no need to create modules if you don't want to.

To answer your specific questions:

The module declaration (module-info.java) needs to go into your source root directory (e.g. src/main/java). It then has to be among the list of files to compile, so it will get turned into a module descriptor (module-info.class). Last step is to include it in the list of class files that are packaged into a JAR. (Having a module descriptor in a JAR turns it into a modular JAR. If placed on the module path, the JPMS turns it into a module.)

If you don't want to create modules after all and prefer your code to run in the unnamed module, you can allow it to access internal APIs with the placeholder ALL-UNNAMED - in the case f your warning you need to open that package to reflection:

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

The better solution would be to stop using internal APIs, though. 😉 As this is not your code, but your dependency, you should look for or open an issue with Apache Felix that asks to remove the access of internal API.

like image 188
Nicolai Parlog Avatar answered Sep 18 '22 09:09

Nicolai Parlog