Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use module-info.java if target level is 8?

I know that module-info.java is recognized by jdk9+

I wonder however whether module-info.java can still be used in combination with target level 8.

E.g. I want my library to be used in java9+ projects, which will recognize it as a module exporting specific packages, but also be used in java8 projects as a plain jar file.

like image 350
Marinos An Avatar asked Jan 09 '19 17:01

Marinos An


People also ask

Is module-info .Java mandatory?

Yes, module-info. java was introduced in Java 9, with the Project Jigsaw module system. A module is a build artifact (usually a Jar file) that contains a module descriptor that declares the name of the module, what other modules it depends on, what packages it exposes to other modules, and what services it implements.

Does Java 8 have modules?

Java 8 with Project Jigsaw brings a module system to the SDK. I see it as a good thing as it's part of the package (built-in). OSGi also provides a module system, but requires a container. But apart from that what are the major difference among them.

Where should module-info be?

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.

What is the use of module-info Java?

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

The Multi-Release JAR files are meant for the exact same purpose itself.

In a JDK that does not support MRJARs, only the classes and resources in the root directory will be visible, and the two packagings will be indistinguishable.

In a JDK that does support MRJARs, the directories corresponding to any later Java platform release would be ignored; it would search for classes and resources first in the Java platform-specific directory corresponding to the currently-running major Java platform release version, then search those for lower versions, and finally the JAR root.

On a Java 9 JDK, it would be as if there were a JAR-specific class path containing first the version 9 files, and then the JAR root; on a Java 8 JDK, this class path would contain only the JAR root.

For an example of this based on Maven, take a look at this - maven-jep238.

like image 98
Naman Avatar answered Sep 30 '22 14:09

Naman