{"id":6009,"date":"2024-10-11T11:55:24","date_gmt":"2024-10-11T11:55:24","guid":{"rendered":"https:\/\/www.aegissofttech.com\/insights\/?p=6009"},"modified":"2025-10-02T06:48:15","modified_gmt":"2025-10-02T06:48:15","slug":"java-platform-module-system-jpms-in-java-9","status":"publish","type":"post","link":"https:\/\/www.aegissofttech.com\/insights\/java-platform-module-system-jpms-in-java-9\/","title":{"rendered":"Java Platform Module System (JPMS) in Java 9 and Beyond"},"content":{"rendered":"\nThe Java platform module system was introduced in <strong>Java 9<\/strong> and was designed to make the platform more secure and scalable. It also allows developers to define modules for better encapsulation and dependency management. It comes up with a major change in the Java 9 version. The work is to collect the Java packages and code into a single unit called a module. To use only the required modules for our project, Java restructured JDK into a set of modules. Besides JDK, it also allows us to create our modules so that developers can create module-based applications easily.\n\nModule system contains various tools given below:\n\n<ul>\n<li>Introduction of Module JAR file, which contains a module-info class file in the root folder.<\/li>\n<li>Introduction of JMOD format, a packaging format similar to JAR, which contains native code and configuration files.<\/li>\n<li>For the improvement of other factors such as performance, security JDK and JRE both are reconstructed to accommodate modules.<\/li>\n<\/ul>\n\n<h2><a name=\"section-1\"><\/a>1. What is a module? <\/h2>\n\nIt is a named grouping of code like classes and interfaces and other resources like images, files, etc. Every module declares what it exports and what it depends on (requires). In other words, a module is a collection of <a href=\"https:\/\/www.aegissofttech.com\/java-outsourcing.html\">Java outsourcing<\/a> programs or software. module-info.java file is required to describe a module. This file is also called a descriptor, and it defines the following:\n\n<ul>\n<li>Module name<\/li>\n<li>What does a module exports<\/li>\n<li>What does a module require<\/li>\n<\/ul>\n\n<h2><a name=\"section-2\"><\/a>2. Creating a Java Module<\/h2>\n\nWe can create a module by following some simple steps.\n\n<ul>\n<li>Create a Directory Structure: We have to follow a reverse domain pattern similar to how we create packages in Java.<\/li>\n<li>Create a Module Declarator: Create a file named module-info.java as a module declarator, and inside the file create a module using the <code>module<\/code> identifier. After this, use the module name the same as a directory name, and if the file has no dependency, leave it empty.<\/li>\n<li>Java source code: Here we can create the source code file with the name<code>Main.java<\/code> and save it.<\/li>\n<\/ul>\n\n<h3>2.1 Example<\/h3>\n\n<h4>2.1.1 Creating a Directory Structure<\/h4>\n\n<pre class=\"brush:plain; wrap-lines:false;\">\nxyz\n\u2502\n\u251c\u2500\u2500 src\n\u2502   \u2514\u2500\u2500 module-info.java\n\u2502   \u2514\u2500\u2500 com\n\u2502        \u2514\u2500\u2500 xyz\n\u2514\u2500\u2500 out (generated by the compiler)\n<\/pre>\n\n<h4>2.2.2 Creating a Module Declarator<\/h4>\n\nHere we need to declare the module name, like here we select &#8216;xyz&#8217;.\n\n<pre class=\"brush:java; wrap-lines:false;\">\nmodule com.xyz {\n  \/\/body\n}\n<\/pre>\n\n<h4>2.2.3 Creating a Source Code File<\/h4>\n\nHere we can create a source code file named <code>Main.java<\/code> and save it.\n\n<pre class=\"brush:java; wrap-lines:false;\">\n\/\/Creating a source code file in Java\nclass Main {\n  public static void main(String args[]) {\n    System.out.println(\"Hello Visitors...!!\");\n  }\n}\n<\/pre>\n\n<h4>2.2.4 Output<\/h4>\n\nHere we get the output generated by the compiler:\n\n<pre class=\"brush:java; wrap-lines:false;\">\nHello Visitors...!!\n<\/pre>\n\n<h2><a name=\"section-3\"><\/a>3. Modules vs. JARs<\/h2>\n\nTraditionally, JARs are used to package Java code, as they don&#8217;t provide the same level of control over dependencies and visibility. The module is a much more advanced concept, and a JAR can contain a module by including the<code>module-info.java<\/code> file.\n\n<h2><a name=\"section-4\"><\/a>4. Features of JPMS<\/h2>\n\n<ul>\n<li>Strong Dependency Management: Some modules can be dependent on other modules as well. This can be part of the module definition itself. Modules explicitly declare the other modules, and then they depend on the usage of the <code>require<\/code> keyword. This ensures that the module system can manage dependencies at compile and runtime.<\/li>\n<li>Strong Encapsulation: Implementation details can be hidden by modules; only the explicitly declared public packages by modules are accessible to other modules. The main purpose is that the encapsulated code may change freely without affecting the module&#8217;s users.<\/li>\n<li>Performance enhancement: Allows better optimization at runtime because of its strong and more explicit structure.<\/li>\n<\/ul>\n\n<h2><a name=\"section-5\"><\/a>5. Modular JDK<\/h2>\n\nJDK (Java Development Kit) in terms of Java Platform Module System refers to the JDK being divided into a set of modules. Depending on the needs of the application each of which can be included or excluded. It was a monolithic system before JAVA 9, all the components are bundled together. The modular JDK breaks this monolith into separate modules. Each module is a self-contained package that contains related classes and resources. <code>javac<\/code> command is used by the developers with module options to compile modular applications and the java command with module option to run module applications. \n\n<h3>5.1 Example<\/h3>\n\n<h4>5.1.1 Create a module-info.java file<\/h4>\n\nThis file is created to define the module and its dependencies. Make sure to create this file in the root of your src directory.\n\n<pre class=\"brush:java; wrap-lines:false;\">\nmodule com.example.myModule {\n  \/\/ Mention all the exported packages\n  exports com.example.myModule.services;\n\n  \/\/ Mention all the required modules here.\n  requires java.sql;\n}\n<\/pre>\n\n<h4>5.1.2 Code Breakdown<\/h4>\n\n<ul>\n<li>Our very first step is to declare the module named com.example.myModule<\/li>.\n<li>Secondly, we make the com.example.myModule.service package available to other modules also.<\/li>\n<li>At last we specify the module, which depends on the Java.sql module.<\/li>\n<\/ul>\n\n<h4>5.1.3 Structure your project<\/h4>\n\nIt helps in managing the module&#8217;s source code and resources. Source files are organized into directories that mirror the package structure with the module-info file.\n\n<pre class=\"brush:plain; wrap-lines:false;\">\nmyModule\n\u2502\n\u251c\u2500\u2500 src\n\u2502   \u2514\u2500\u2500 module-info.java\n\u2502   \u2514\u2500\u2500 com\n\u2502       \u2514\u2500\u2500 example\n\u2502           \u2514\u2500\u2500 myModule\n\u2502               \u2514\u2500\u2500 service\n\u2502                   \u2514\u2500\u2500 MyService.java\n\u2514\u2500\u2500 out (generated by the compiler)\n<\/pre>\n\n<h4>5.1.4 Implementation of classes<\/h4>\n\nHere, Create classes within the packages you declared in your module.\n\n<pre class=\"brush:java; wrap-lines:false;\">\npackage com.example.myModule.services;\n\n public class MyService {\n   public void printMessage() {\n     System.out.println(\"Hello Visiters...!!\");\n   }\n }\n<\/pre>\n\n<h4>5.1.5 Compilation of Module<\/h4>\n\n<ul>\n<li>In order to compile the module, we use the <code>javac<\/code> command.<\/li>\n<li>The command compiles all the <code>.java<\/code> file in the src directory and then places the compiled classes in the out directory, which results in preserving the module structure.<\/li>\n<\/ul>\n\n<pre class=\"brush:plain; wrap-lines:false;\">\njavac -d out --module-source-path src $(find src -name\"*.java\")\n<\/pre>\n\nThis is a very basic example of setting up and using the Java Platform Module System. We can build more complex modules by using advanced dependencies and services as your application grows.\n\n<h2><a name=\"section-6\"><\/a> 6. Conclusion<\/h2>\n\nInitially, in the development phase, the Java Platform Module System was also named Project Jigsaw. It makes the task easier for the developers to organize large applications and libraries by increasing the structure and level of security of the platform. It is very useful in enhancing the performance of any application and for the decomposition of the platform for smaller devices. The main idea behind this is to enable the collection of related packages that are visible to the module while hiding the elements from external consumers on the module. It permits <a href=\"https:\/\/www.aegissofttech.com\/hire-java-developers.html\">Java developers India<\/a> to efficiently structure their applications by organizing code into modules.\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":" ","protected":false},"author":12,"featured_media":6014,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[12],"tags":[966],"class_list":["post-6009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java-platform-module-system-jpms-in-java-9-and-beyond"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts\/6009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/comments?post=6009"}],"version-history":[{"count":4,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts\/6009\/revisions"}],"predecessor-version":[{"id":14780,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/posts\/6009\/revisions\/14780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/media\/6014"}],"wp:attachment":[{"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/media?parent=6009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/categories?post=6009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aegissofttech.com\/insights\/wp-json\/wp\/v2\/tags?post=6009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}