Java 9 模块
Java 9 模块是 Java 结构中最大的变化之一。这里我将对“Java 9 模块系统”进行简要的介绍。
Java 9 模块
我们将研究有关 Java 9 模块系统的以下主题。
- 介绍
- Java SE 9:拼图项目
- 当前 Java 系统存在什么问题?
- Java SE 9 模块系统的优势
- 比较 JDK 8 和 JDK 9
- 什么是 Java 9 模块?
- Java 9 模块系统之母
- 比较 Java 8 和 Java 9 应用程序
介绍
Oracle 公司已将Java 9的发布时间从 2017 年 3 月推迟到2017 年 9 月。众所周知,Java SE 8 带来了三大新功能(以及一些增强功能和新功能)。
- Lambda 表达式
- 流 API
- 日期 API
同样,Java SE 9 具有三大特性(另外还有一些增强功能和新功能):
- Java 模块系统(Jigsaw 项目)
- Java REPL
- 铣削项目硬币
在本文中,我们将讨论 Oracle Jigsaw 项目基础知识。
Java SE 9:拼图项目
Jigsaw 项目将引入 Java SE 9 的全新概念:Java 模块系统。这是 Oracle 公司在 Java SE 9 版本中推出的一个非常大且声名显赫的项目。最初,他们将这个项目作为 Java SE 7 版本的一部分启动。但是,由于变化巨大,它被推迟到 Java SE 8,然后又被推迟。现在它将于 2017 年 9 月与 Java SE 9 一起发布。Jigsaw项目的主要目标:
- 模块化 JDK 众所周知,当前的JDK系统太大。因此,他们决定将 JDK 本身分成小的模块,以获得许多好处(我们将在接下来的章节中讨论它们)。- 模块化源代码当前源代码jar文件太大,尤其是 rt.jar 太大了。所以他们将把 Java 源代码分成更小的模块。- 模块化运行时映像此功能的主要目标是“重构 JDK 和 JRE 运行时映像以适应模块”。- 封装大多数内部 API 此功能的主要目标是“使大多数 JDK 的内部 API 默认情况下不可访问,但保留一些关键的、广泛使用的内部 API 可访问”。- Java 平台模块系统此功能的主要目标是“允许用户创建自己的模块来开发应用程序”。- jlink:Java 链接器此 jlink 工具的主要目标是“允许用户为其应用程序创建可执行文件”。
如果你对这些内容不太理解,请不要担心。我们将在接下来的章节以及我接下来的帖子中通过一些有用的例子深入讨论这些概念。
当前 Java 系统存在什么问题?
在本节中,我们将讨论“为什么我们需要 Java SE 9 模块系统”,这意味着当前 Java 系统存在问题。Java SE 8 或更早的系统在开发或交付基于 Java 的应用程序时存在以下问题。
- As JDK is too big, it is a bit tough to scale down to small devices. Java SE 8 has introduced 3 types of compact profiles to solve this problem: compact1, compact2, and compact3. But it does not solve this problem.
- JAR files like rt.jar etc are too big to use in small devices and applications.
- As JDK is too big, our applications or devices are not able to support better Performance.
- There is no Strong Encapsulation in the current Java System because “public” access modifier is too open. Everyone can access it.
- As JDK, JRE is too big, it is hard to Test and Maintain applications.
- As the public is too open, They are not to avoid the accessing of some Internal Non-Critical APIs like sun.*, *.internal.* etc.
- As User can access Internal APIs too, Security is also big issue.
- Application is too big.
- Its a bit tough to support Less Coupling between components.
To solve all these problems, Oracle Corp is going to release Java Module system in Java SE 9 Release.
Advantages of Java SE 9 Module System
Java SE 9 Module System is going to provide the following benefits
- 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.
- As public is not just public, it supports very Strong Encapsulation. (Don’t worry its a big concept. we will explore it with some useful examples soon).
- We cannot access Internal Non-Critical APIs anymore.
- Modules can hide unwanted and internal details very safely, we can get better Security.
- Application is too small because we can use only what ever modules we want.
- Its easy to support Less Coupling between components.
- Its easy to support Single Responsibility Principle (SRP).
We will explore all these concepts one by one soon.
Compare JDK 8 and JDK 9
We know what a JDK software contains. After installing JDK 8 software, we can see a couple of directories like bin, jre, lib etc in Java Home folder. However, Oracle Corp has changed this folder structure a bit differently as shown below. JDK 8 Folder Structure: JDK 9 Folder Structure: Here JDK 9 does NOT contain JRE. In JDK 9, JRE is separated into a separate distribution folder. JDK 9 software contains a new folder “jmods”. It contains a set of Java 9 Modules as shown below. In JDK 9, No rt.jar and No tools.jar NOTE:- As of today, “jmods” contains 95 modules. It may increase in the final release. “jmods” folder is available at ${JAVA_HOME}/jmods. These are known as JDK Modules.
What is Java 9 Module?
A Module is a self-describing collection of Code, Data, and some Resources. It is a set of related Packages, Types (classes, abstract classes, interfaces etc) with Code & Data and Resources. Each Module contains only a set of related code and data to support Single Responsibility (Functionality) Principle (SRP). The main goal of Java 9 Module System is to support Modular Programming in Java. We will discuss on “What is a Module Descriptor” and “How to develop Java Modules” in my coming posts.
Mother of Java 9 Module System
As of now, Java 9 Module System has 95 modules in Early Access JDK. Oracle Corp has separated JDK jars and Java SE Specifications into two set of Modules.
- All JDK Modules starts with “jdk.*”
- All Java SE Specifications Modules starts with “java.*”
Java 9 Module System has a “java.base” Module. It’s known as Base Module. It’s an Independent module and does NOT dependent on any other modules. By default, all other Modules dependent on this module. That’s why “java.base” Module is also known as The Mother of Java 9 Modules. It’s default module for all JDK Modules and User-Defined Modules.
Compare Java 8 and Java 9 Applications
We have already developed many Java applications using Java 5, 6,7, or 8. We know how a Java 8 or earlier applications looks like and what it contains. In brief, I have depicted a Java 8 applications in a diagram as shown below: In a Java 8 or earlier applications, Top level component a Package. It groups a set related to types into a group. It also contains a set of resources. Java 9 Applications does not have much difference with this. It just introduced a new component called “Module”, which is used to group a set of related Packages into a group. And one more new component that Module Descriptor (“module-info.java”). That’s it. Rest of the application is same as earlier versions of applications as shown below. Like Java 8 applications have Packages as a Top level components, Java 9 applications have Module as Top Level components. NOTE:- Each Java 9 Module have one and only one Module and one Module Descriptor. Unlike Java 8 Packages, We cannot create multiple modules into a single Module. In brief I can say a Java 9 Module contains the following main components:
- One Module
- Module Name
- Module Descriptor
- Set of Packages
- Set of Types and Resources
Here Resources may be module-info.java (Module Descriptor) or any other properties or XML. We have discussed too much theory in this post. I think that is enough to understand the basics of Java 9 Module system. We will start some basic Module development in my coming post. I will discuss some more Theory concepts or Java 9 Module System terminology as and when required. That’s it all about “Introduction to Java 9 Module System” topic. We will discuss some more Java SE 9 Module System concepts in my coming posts. Please drop me a comment if you like my post or have any issues/suggestions/type errors. Thank you for reading my tutorials. Happy Java SE 9 Learning! Reference: Project Jigsaw