Why Design Jigsaw / JPMS?

The Java package management system has always seemed simple and efficient to me. It is widely used by JDK itself. We used it to simulate the concept of namespaces and modules.

What is a Jigsaw Project (aka Java Platform Module System) trying to populate?

From the official website:

The aim of this project is to develop and implement a standard system module for the Java SE platform and apply this system to the platform itself and the JDK.

+68
java java-9 jigsaw
Aug 07 '12 at 11:20
source share
5 answers

Jigsaw and OSGi are trying to solve the same problem: how to allow coarse-grained modules to interact, protecting their insides.

In the case of Jigsaw, coarser modules include Java classes, packages, and their dependencies.

Here is an example: Spring and Hibernate. Both are dependent on a third-party CGLIB JAR, but they use different, incompatible versions of this JAR. What can you do if you rely on the standard JDK? Including the version that Spring wants sleep breaks and vice versa.

But, if you have a higher-level model, such as Jigsaw, you can easily manage different versions of the JAR in different modules. Think of them as higher-level packages.

If you build Spring from a GitHub source, you will see it too. They redid the structure, so it consists of several modules: core, persistence, etc. You can choose the minimum set of module dependencies your application needs and ignore the rest. It used to be the only Spring JAR, with all .class files.

Update: Five Years Later - Jigsaw may still have problems.

+92
Aug 07 '12 at 11:27
source share

AFAIK The plan is to make the JRE more modular. That is, they have smaller banks that are optional and / or you can download / update only those functions that you need.

To make it less bloated and give you the option to abandon legacy modules that most people probably don't use.

+40
Aug 07 '12 at 11:27
source share

Based on Mark Reinhold's main speech in Devoxx Belgium , Project Jigsaw is going to eliminate two main pain points:

  • Classpath
  • Massive Monolithic JDK

What happened to Classpath?

We all know about JAR Hell . This term describes all the different ways in which the class loading process may not work. The most famous classpath restrictions are:

  • It is hard to say if there are conflicts. Building tools like maven can do pretty good work based on the names of the artifacts, but if the artifacts themselves have different names but the same content, a conflict can arise.
  • The main problem with jar files is that they are not components. This is just a bunch of file containers that will run linearly. Classpath is a way to search for classes regardless of what components they are in, what packages they are in or their intended use.

Massive Monolithic JDK

The large monolithic nature of JDK causes several problems:

  • It is not suitable for small devices. Despite the fact that small devices such as IoT have processors capable of running a virtual machine of class SE, but they do not have memory to store the entire JDK, especially when the application uses only a small part.
  • This is even a problem in the Cloud. Cloud is all that is needed to optimize the use of hardware, if you have thousands of images containing the entire JDK, but applications use only a small part, this will be a waste.

Modules: General Solution

To solve the above problems, we consider modules as a fundamental new kind of Java component program. A module is a named, self-describing collection of code and data. Its code is organized as a collection of packages containing types, that is, classes and Java interfaces; its data includes resources and other types of static information.

To control how its code relates to types in other modules, the module announces which other modules it requires to compile and run. To control how the code in other modules relates to types in their packages, the module announces which of these packages it exports.

The module system finds the necessary modules and, unlike the class mechanism, ensures that the code in the module can only refer to the types in the modules on which it depends. The Java access control mechanisms and the Java virtual machine prevent codes from accessing types in packages that are not exported using defining modules.

In addition to greater reliability, modularity can increase productivity. When the code in the module is of the type in the package, then this package is guaranteed to be defined either in this module or in exactly one of the modules read by this module. When searching for definitions of a certain type, therefore, there is no need to search for it in several modules or, even worse, along the whole class path.

Jep to execute

Jigsaw is a huge project that has been going on for several years. He got an impressive amount of JEPs, which are great places to get more information about the project. Some of these JEPs are as follows:

  • JEP 200: Modular JDK : Use the Java Platform Module (JPMS) system to modulate the JDK
  • JEP 201: Modular source code : reorganize JDK source code into modules, improve the build system for compiling modules, and ensure that the boundaries of the module are respected at build time
  • JEP 261: Module System : Deploying the Java Platform Module system as specified by JSR 376, along with JDK-related changes and improvements
  • JEP 220: Modular Runtime Images : Restructure JDK and JRE runtime images to accommodate modules and improve performance, security, and maintainability
  • JEP 260: encapsulate most internal APIs : make most internal JDK APIs unavailable by default, but leave some critical, widely used internal APIs available until supported replacements exist for all or most of their functionality.
  • JEP 282: jlink: Java Linker : Create a tool that can assemble and optimize a set of modules and their dependencies at custom runtime, as defined in JEP 220

Concluding observations

In the initial issue of the Module Status report , Mark Reinhold describes the specific objectives of the module system as follows:

  • A robust configuration to replace the fragile, error-prone class path mechanism using software components to declare explicit dependencies with each other along with
  • Strong encapsulation to allow a component to declare which of its public types is available to other components and which are not.

These features will help application developers, library developers, and developers of the Java SE platform itself, as well as indirectly, as they allow a scalable platform, greater platform integrity, and improved performance.

+35
Jan 15 '16 at
source share

For the argument, let it be argued that Java 8 (and earlier) already has the “shape” of modules (jars) and a modular system (class path). But problems are well known with them.

By exploring problems, we can illustrate the motivation of Jigsaw. (It is assumed that we do not use OSGi, JBoss Modules, etc., which certainly offer solutions.)

Problem 1: the public is too public

Consider the following classes (suppose both are publicly available):

com.acme.foo.db.api.UserDao com.acme.foo.db.impl.UserDaoImpl 

At Foo.com, we can decide that our team should use UserDao and not use UserDaoImpl directly. However, there is no way to enforce this path in the classpath.

In Jigsaw, a module contains the module-info.java file, which allows us to explicitly indicate what is publicly available for other modules. That is, the public has a nuance. For example:

 // com.acme.foo.db.api.UserDao is accessible, but // com.acme.foo.db.impl.UserDaoImpl is not module com.acme.foo.db { exports com.acme.foo.db.api; } 

Problem 2: unbridled reflection

Given the classes in # 1, someone can still do this in Java 8:

 Class c = Class.forName("com.acme.foo.db.impl.UserDaoImpl"); Object obj = c.getConstructor().newInstance(); 

That is: the reflection is powerful and significant, but if it is not checked, it can be used for unwanted penetration into the internal parts of the module. Mark Reinhold has a rather disturbing example . (SO post here .)

In Jigsaw, strong encapsulation makes it possible to deny access to the class, including reflection. (This may depend on the command line settings in anticipation of a revised technology specification for JDK 9.) Note that since Jigsaw is used for the JDK itself, Oracle claims that this will allow the Java team to quickly implement the internal platform environment.

Problem 3: the class path erases architectural relationships

The team usually has a mental model about the relationship between the jars. For example, foo-app.jar can use foo-services.jar , which uses foo-db.jar . We can argue that classes in foo-app.jar should not bypass the "service level" and use foo-db.jar directly. However, there is no way to provide this via the classpath. Mark Reinhold mentions here .

In comparison, Jigsaw offers a clear and reliable accessibility model for modules.

Problem 4: monolithic runtime

Java runtime is in monolithic rt.jar . On my machine, it's 60+ MB with 20k classes! In the era of microposts, IoT devices, etc. It is undesirable to have Corba, Swing, XML files and other libraries on disk if they are not used.

Jigsaw breaks JDK itself into many modules; for example java.sql contains familiar SQL classes. There are several advantages to this, but the new jlink tool. Assuming the application is fully modular, jlink creates a distributed image at runtime that is cropped to contain only the specified modules (and their dependencies). Looking ahead, Oracle foresees a future where JDK modules are compiled on time into native code. Although jlink is optional, and AOT compilation is experimental, they are key indicators of where Oracle is heading.

Problem 5: version

It is well known that the path to classes does not allow the use of several versions of the same bank: for example. bar-lib-1.1.jar and bar-lib-2.2.jar .

Jigsaw does not solve this problem; Mark Reinhold argues the rationale here . The bottom line is that Maven, Gradle, and other tools are a great ecosystem for dependency management, and another solution would be more harmful than profitable.

It should be noted that other solutions (e.g. OSGi) do address this issue (and others other than # 4).

Bottom row

These are some key issues for Jigsaw that are caused by specific problems.

Please note that explanation of disputes between Jigsaw, OSGi, JBoss Modules, etc. is a separate discussion owned by another Stack Exchange site. There are much more differences between the solutions than described here. Moreover, sufficient consensus was reached on the approval of the public review revision bulletin for JSR 376.

+6
Jun 15 '17 at 18:03
source share

This article details the problems that both Gi and JPMS / Jigsaw are trying to solve:

"Java 9, OSGi and the future of modularity" [22 SEP 2016]

It also fully fits into the approaches of both OSGi and JPMS / Jigsaw. At the moment, apparently, the authors list almost no practical "Pros" for JPMS / Jigsaw compared to the mature (16 years) OSGi.

+2
Nov 11 '16 at 6:20
source share



All Articles