Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mix Java 8 and Java 9 source code in the same project without using compiler flags?

In Java 9, you can optionally package a source directory as a module by adding a module-info.java, which defines the things packages that it exports, and which other modules it depends on.

Once you do that, however, you must list ALL dependencies in the module descriptor - and the dependencies must all themselves be modules. Therefore, by extension, if you modularize a single source directory, you must modularize every single source directory company wide.

Furthermore, you cannot define modules in Java 8 or earlier, meaning that in addition to modularizing every single Java source directory, you must convert everything to Java 9. At the same time.

This seems catastrophic if you work in a company with a large base of code that is shared by many different projects.

For now, I can work around the problem by just setting a bunch of compiler flags to avoid defining modules, but that seems like a very poor solution.

I hope that I am understanding this incorrectly?

like image 457
Jesse Barnum Avatar asked Nov 17 '17 20:11

Jesse Barnum


People also ask

What is the difference between Java 8 and 9?

Java 8 applications use packages as a top-level component whereas Java 9 applications use modules as a top-level component. Each Java 9 module has only one module with one module descriptor whereas Java 8 package doesn't build multiple modules into a single module.

Can a jar contain multiple modules?

Each module-info. java which lives in the default package will be compiled to module-info. class . Therefore, one JAR cannot contain multiple modules.

What is Java jigsaw?

Project Jigsaw is an umbrella project with the new features aimed at two aspects: the introduction of module system in the Java language. and its implementation in JDK source and Java runtime.


2 Answers

Once you do that, however, you must list ALL dependencies in the module descriptor

True.

and the dependencies must all themselves be modules.

Technically true, but it doesn't imply what you think it does.

Therefore, by extension, if you modularize a single source directory, you must modularize every single source directory company wide.

No, because you can let the module system turn regular old JARs into automatic modules, which will get a name based on a manifest entry or their file name - you can find that out with:

# jar command from Java 9
jar --describe-module --file $JAR_FILE

Furthermore, you cannot define modules in Java 8 or earlier, meaning that in addition to modularizing every single Java source directory, you must convert everything to Java 9. At the same time.

Again, fortunately that's not quite right. You can add a module-info.class to a JAR built for Java 8 and it will work on both Java 8 (which ignores that file) and Java 9 (which can of course execute Java 8 bytecode).

like image 125
Nicolai Parlog Avatar answered Oct 03 '22 19:10

Nicolai Parlog


if you modularize a single source directory, you must modularize every single source directory company-wide.

No, that does not hold true, for the fact, that is what automatic modules are designed for and

(to favor the impulse) Yes, eventually that shall be the goal of modularisation.


Reiterating the need of introducing the automatic modules from The State of the Module System:

Bottom-up migration is straightforward, but it is not always possible. Even if the maintainer of org-baz-qux.jar has not yet converted it into a proper module—or perhaps never will—we might still want to modularize our com-foo-app.jar and com-foo-bar.jar components.


When you actually say :

you can optionally package a source directory as a module by adding a module-info.java

you tend to migrate that artifact into a module(with module description) and place such modules in the module-path of the libraries using this artifact further.

On the other hand, a .jar of your library without the module-info.class is considered to be present at the class-path when included as a dependency in downstream projects.


Edit from comments:-

it's possible to mix Java 9 source with Java 8 compiled jars, but you can't compile a single project containing Java 9 source and Java 8 source?

Yes, its possible to mix Java9 source with Java8 compile jar and you can still compile them into a single project as well.

Example:- take a look at how Maven does this using maven-compiler-plugin for Java8 projects with module-info.java included.

like image 37
Naman Avatar answered Oct 03 '22 19:10

Naman