Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java 9 does not simply turn all JARs on the class path into automatic modules?

In order to understand the categories we have:

  • platform explicit modules
  • application explicit modules
  • open modules
  • automatic modules
  • unnamed module

All classes and jars within the classpath will be part of the unnamed module. But why is that what we need? Where is the advantage over automatic modules? I could "require" those damn legacy jars to make them to an automatic module. Do I not have included everything with it?

like image 893
Daniel Avatar asked Jul 18 '17 12:07

Daniel


People also ask

What happened to the classpath in Java 9?

In Java 9 the classpath gets completely revamped with the new Jigsaw modularity system. In Java 9 a jar can be declared as a module and it will run in it's own isolated class loader, that reads class files from other similar module class loaders in an OSGI sort of way.

What is the difference between class path and class jar?

Since there were never any boundaries between JARs on the class path, it makes no sense to establish them now and so there's a single unnamed module for the entire class path. Within it, just like on the class path, all public classes are accessible to one another and packages can be split across JARs.

What is the use of jar in Java 9?

In Java 9 a jar can be declared as a module and it will run in it's own isolated class loader, that reads class files from other similar module class loaders in an OSGI sort of way. This will allow multiple versions of the same Jar to coexist in the same application if needed.

How do I launch a modular app in Java?

To launch a modular app, run the java command with a module path and a so-called initial module - the module that contains the main method: This will start a process called module resolution : Beginning with the initial module's name, the module system will search the module path for it.


1 Answers

There are at least two reasons:

  • Just as regular modules, automatic ones are suspect to certain examinations by the module system, e.g. not splitting packages. Since JARs on the class path can (and occasionally do) split packages, imposing that check on them would be backwards-incompatible and break a number of applications.
  • The unnamed module can read all platform modules, whereas automatic modules can only read those that made it into the module graph. That means a JAR needing the java.desktop module (for example) will work from the class path but not from the module graph unless java.desktop also makes it into the graph (via a dependency or --add-modules).

I have no time right now to check the second but that's what the State of the Module system says:

After a module graph is resolved, therefore, an automatic module is made to read every other named module, whether automatic or explicit

Resolution works on the declared dependencies and an automatic modules declares none.

like image 95
Nicolai Parlog Avatar answered Sep 19 '22 13:09

Nicolai Parlog