Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of java.se module in Java 9?

Why does java 9 module system has java.se module which has transitive dependencies on other modules. Isn't it same as depending upon entire rt.jar in pre Java 9 world.

module java.se {
    requires transitive java.desktop;
    requires transitive java.security.jgss;
    requires transitive java.security.sasl;
    requires transitive java.management;
    requires transitive java.logging;
    requires transitive java.datatransfer;
    requires transitive java.sql.rowset;
    requires transitive java.compiler;
    requires transitive java.sql;
    requires transitive java.naming;
    requires transitive java.prefs;
    requires transitive java.rmi;
    requires transitive java.xml.crypto;
    requires transitive java.management.rmi;
    requires transitive java.xml;
    requires transitive java.scripting;
    requires transitive java.instrument;
}
like image 910
sumit sachdeva Avatar asked May 25 '17 10:05

sumit sachdeva


People also ask

What is Java SE module?

Defines the API of the Java SE Platform. Optional for the Java SE Platform: Java Native Interface (JNI) Java Virtual Machine Tool Interface (JVM TI) Java Debug Wire Protocol (JDWP)

What is a module system in Java 9?

Defining the Java 9 module. A module is a collection of code, data, and resources. It is a set of related packages and types (classes, abstract classes, interfaces, and more) with code, data files, and some static resources.

What are the benefits of Java modules?

Advantages of Java SE 9 Module System As Java SE 9 is going to divide JDK, JRE, JARs etc, into smaller modules, we can use whatever modules we want. So it is very easy to scale down the Java Application to Small devices. Ease of Testing and Maintainability. Supports better Performance.


2 Answers

As far as I can tell the main reason is compatibility with non-modular Java EE code. When code that has no module declaration or descriptor (which defines its dependencies) is compiled or launched, the question arises which modules from the JDK it is allowed to "see".

If those were all modules in the JDK, then the Java EE ones would "overshadow" any Java EE implementations that are put onto the class path. This is a peculiarity of the interaction between modules and the class path (which ends up in the unnamed module): if a package is present in both a regular module and the unnamed one, the latter will be effectively invisible.

To remedy that fact not all modules will be visible for code on the class path. Instead, module resolution (which resolves an application's dependencies) will begin in the root module java.se, thus ignoring Java EE modules.

For a more detailed explanation, have a look at this mail from Alan Bateman, in which he explains the change of the corresponding JEP 261.

like image 81
Nicolai Parlog Avatar answered Sep 28 '22 15:09

Nicolai Parlog


The java.se module is the default set of modules (aka "root modules") available to applications in Java 9. You are right that the java.se module is about 80% of what was in the rt.jar of older JDKs (if you want to full rt.jar take a look at the java.se.ee module).

One key advantage of Java 9 modularity is the new jlink utility, which will "minify" your JDK to only the modules that are needed. For example, suppose you write a very simple Java app that only depends on the java.base module and you want to distribute in the smallest possible form. You can run the jlink utility to create a JDK image that would contain only contain the java.base module, because that's all your application requires in order to run.

Getting back to the java.se module, it would probably be foolish to write a module that requires java.se because this would ruin the possibility of jlink ever creating a nice small JDK image for that module or any other modules that depended on it. In an ideal world, all modules declare the precise modules that they require.

like image 44
Andy Guibert Avatar answered Sep 28 '22 15:09

Andy Guibert