<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jubin on Medium]]></title>
        <description><![CDATA[Stories by Jubin on Medium]]></description>
        <link>https://medium.com/@jubin.kuriakose?source=rss-11dd2289eeb6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2ZxnoU6iwmyrBadiaurUbQ.jpeg</url>
            <title>Stories by Jubin on Medium</title>
            <link>https://medium.com/@jubin.kuriakose?source=rss-11dd2289eeb6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 08:20:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jubin.kuriakose/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java Module System — Part II]]></title>
            <link>https://medium.com/@jubin.kuriakose/java-module-system-part-ii-f051f2110a9e?source=rss-11dd2289eeb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/f051f2110a9e</guid>
            <category><![CDATA[modular-programming]]></category>
            <category><![CDATA[java9]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Jubin]]></dc:creator>
            <pubDate>Sat, 25 May 2019 00:56:09 GMT</pubDate>
            <atom:updated>2019-05-25T02:07:19.833Z</atom:updated>
            <content:encoded><![CDATA[<h3>Java Module System — Part II</h3><p>This is a continuation of the module system introduced to Java 9 . You can find Part — I at the link below:</p><p><a href="https://medium.com/@jubin.kuriakose/modular-programming-in-java-42788ec02268">Modular programming in Java</a></p><h3>Nuances</h3><p>The module system comes with its on set of rules on how it can be accessed. The module should be allowed to access the other module via the <em>requires </em>keyword. The other module should <em>export </em>the package and the type itself should be public (to make it visible)</p><p>The module system also requires that two modules cannot have the same package name. This enforcement separates the module-path from the class-path paradigm where packages could be overridden by multiple jars having same package name. This enforces stricter security on the classes being referred.</p><blockquote>There are also performance optimization with respect to class loading as the JVM no longer needs to search the class-path but rather maps the package the module it needs to pick-up from</blockquote><p>The module system also have set of other directives (apart from <em>requires </em>and <em>export</em>) for managing dependencies.</p><ul><li>transitive</li></ul><p>There are cases where when module A depends on another module B, module A might also require to access types that are actually part of module B’s dependencies by virtue of its requires keyword. This is not possible out of the box as dependencies are not transitive.</p><blockquote>Module A requires Module B requires Module C, doesn’t mean Module A automatically requires Module C</blockquote><p>To make this happen we would need to use the transitive keyword</p><pre><strong>module </strong>com.jtk.nutrition.lib {<br>    <strong>requires transitive </strong>com.jtk.nutrition.loader;<br>    <strong>requires transitive </strong>com.jtk.service;<br>}</pre><p>The above declaration makes an module that requires com.jtk.nutrition.lib to also have readability to modules com.jtk.nutrition.loader and com.jtk.service (a service module — more on that below). This way the top module (that requires com.nutrition.lib) doesn’t have to <em>require </em>all the dependencies internally.</p><ul><li>aggregator</li></ul><p>The above module in the application also acts as an aggregator, in that it doesn’t <em>have to</em> have any source except for the module-info.java.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mGH54ew2oOvvhjplvqvGKw.png" /></figure><p>In this way we can collate all the dependencies to a particular module and in the main module above ( com.jtk.nutrition.cli) we just <em>require </em>the aggregator: com.jtk.nutrition.lib</p><h3>Services</h3><p>Java module system also introduces the concept of services. These are modules that only exposes the interfaces to the client. The actual implementations are separate modules that uses a special keyword to indicate a service implementation. As shown below our application is enhanced to include a service com.jtk.service and its two implementations com.jtk.validate.number and com.jtk.validate.string</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r3kQMVZvQLuuOWAw-4nY0A.png" /></figure><h4>The client</h4><p>The module com.jtk.service has the a single interface that is invoked by the clients. This invocation is made possible by the uses directive:</p><pre><strong>module </strong>com.jtk.nutrition.cli{<br>    <strong>requires </strong>java.logging;<br>    <strong>requires </strong>com.jtk.nutrition.lib;<br>    <strong>uses com.jtk.service.api.ValidationService;</strong><br>}</pre><p>Unlike dependency injection where the implementation is injected at run-time, here the dependencies are looked up using ServiceLoader.<em>load</em>(ValidationService.<strong>class</strong>);</p><p>This returns a collection of implementations that can be called as such</p><pre>Iterable&lt;ValidationService&gt; validationServices = ServiceLoader.<em>load</em>(ValidationService.<strong>class</strong>);<br><strong>for </strong>(ValidationService s : validationServices) {<br>    s.validate(o)<br>}</pre><p>The problem with this is that, the client might need to call a particular implementation and there is no way to achieve this without having the interface expose a static factory method to retrieve the apt implementation (hidden behind the interface of-course! ). Again this is not dependency injection so you shouldn’t have the same expectations.</p><p>Also for reducing code duplicity, the above code needs to be part of the service interface; to that affect the uses directive is moved to the service module as shown below:</p><pre><strong>module </strong>com.jtk.service {<br>    <strong>exports </strong>com.jtk.service.api;<br>    <strong>uses </strong>com.jtk.service.api.ValidationService;<br>}</pre><p>And the interface exposes a factory method to select the implementation as below:</p><pre><strong>public interface </strong>ValidationService&lt;T&gt; {<br>    <strong>boolean </strong>validate(T t);<br><br>    <strong>boolean </strong>isSupportedType(Object t);<br><br>    <strong>public static </strong>List&lt;ValidationService&gt; <strong>getValidators</strong>(Object item) {<br>        List&lt;ValidationService&gt; validationServiceList = <strong>new </strong>ArrayList&lt;&gt;();<br>        Iterable&lt;ValidationService&gt; validationServices = ServiceLoader.<em>load</em>(ValidationService.<strong>class</strong>);<br>        <strong>for </strong>(ValidationService s : validationServices) {<br>            <strong>if </strong>(s.isSupportedType(item))<br>                validationServiceList.add(s);<br>        }<br>        <strong>return </strong>validationServiceList;<br>    }<br>}</pre><p>Now as the client doesn’t call the ServiceLoader directly and the client module com.jtk.nutrition.cli doesn’t require the uses directive</p><pre><strong>module </strong>com.jtk.nutrition.cli{<br>    <strong>requires </strong>java.logging;<br>    <strong>requires </strong>com.jtk.nutrition.lib;<em>*(note)</em><br>}</pre><p>*The com.jtk.nutrition.lib module is an aggregator of the service and other modules required by the application (see aggregators above)</p><h4>The implementation</h4><p>On the implementation side of things i.e. com.jtk.validate.number and com.jtk.validate.string modules have a special directive that registers itself as services to an interface:</p><pre><strong>module </strong>com.jtk.validate.string {<br>    <strong>requires </strong>com.jtk.service;<br>    <strong>provides </strong>com.jtk.service.api.ValidationService <strong>with </strong>com.jtk.validate.string.FirstCapsValidator;<br>}</pre><pre><strong>module </strong>com.jtk.validate.number {<br>    <strong>requires </strong>com.jtk.service;<br>    <strong>provides </strong>com.jtk.service.api.ValidationService <strong>with </strong>com.jtk.validate.number.DoubleValidation;<br>}</pre><p>The provides - with keyword registers the implementations to the interface as above.</p><p>With that we have registered the implementation com.jtk.validate.string and com.jtk.validate.number to the service module com.jtk.service and have provided a dependency to the client library via the aggregator com.jtk.nutrition.lib as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Ex81eYHkMsDSn1H8PiLsA.png" /><figcaption>modules dependency graph</figcaption></figure><h4>Summary</h4><p>With this we have looked into the various rules regarding modules and how to handle aggregates and transitive dependencies. There are other directives but these are the essential ones to develop a robust modular system. We also looked into how services are created and how they are called with-in the module system.</p><p>In the next and final article on java modular system I intend to explore the jlink binary that comes with the module system in Java 9</p><p>Below is a reference to the source code for the above articles</p><p><a href="https://github.com/jtkSource/jpms-nutritioninfo">jtkSource/jpms-nutritioninfo</a></p><h4>References:</h4><p>I have referred primarily the below book and also other websites in exploring the module system in java. Its a very good read!</p><p><a href="https://www.amazon.com/Modular-Programming-Java-applications-modularity-ebook/dp/B01MR90YR1">Modular Programming in Java 9: Build large scale applications using Java modularity and Project Jigsaw</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f051f2110a9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modular programming in Java]]></title>
            <link>https://medium.com/@jubin.kuriakose/modular-programming-in-java-42788ec02268?source=rss-11dd2289eeb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/42788ec02268</guid>
            <category><![CDATA[jigsaw]]></category>
            <category><![CDATA[java-9-recipes]]></category>
            <dc:creator><![CDATA[Jubin]]></dc:creator>
            <pubDate>Mon, 20 May 2019 17:01:39 GMT</pubDate>
            <atom:updated>2019-05-25T01:08:07.973Z</atom:updated>
            <content:encoded><![CDATA[<h3>Java Module System — Part I</h3><p>Java 9 as we all know introduced the Java Platform Module System (JSR 376). This addresses some of the issues of the existing Java ecosystem.</p><p>The three main issues from a development point of view that the module system in Java 9 tries to address:</p><ol><li>Hiding implementation</li><li>Effectively removing dependency on legacy code for a library</li><li>Sanitizing the Classpath</li></ol><h4>Advantages of JPMS</h4><ol><li>Scalable Platform</li><li>Security &amp; maintainability</li><li>Improved application performance</li><li>Easier developer experience</li></ol><p>So then how does the module system promise all this? To answer the question we should first understand Java’s module system.</p><h4>Sins of the CLASSPATH</h4><p>A traditional java application involves building libraries into jars and placing them on the class-path of the run-time. The run-time resolves the dependencies for the types using the the package name. Once in the class-path, the JVM treats classes in a JAR no differently from separate class files all in the same root directory. At run-time, as far as the JVM is concerned, an application is just a set of classes in a flat list of packages. This leads to a few issues</p><p>Encapsulation is all about hiding what you don’t want to show to the recipient of your library. All your dirty secrets! But the moment you drop your jar into a class-path all your secrets are out! Well at-least the public classes that are meant to be internal to the library. Traditionally library developers used to create the package with the name <em>internal</em> as the unwritten law for types not to be used by clients of the library but there was no enforcement of this.</p><p>Another problem is that as the JVM sees the package name as just names, we could have two jars with same package name and hence compromise the application integrity.</p><p>It could be that the classes that are required for the application to run is missed all together and the run-time throws a NoClassDefFoundError when execution point hits the point where the class is called for.</p><p>Jar Hell like Dll hell is a real issue where in large codebases with a lot of dependencies and the developer has to be extremely careful of what is being extended or called.</p><p>The codebase of our dear JDK is itself 20 years old and because of backward compatibility there is a large amount of the codebase that is pulled in even for a tiny application. This size also has a hit on the performance of the JVM. Even classes for outdated technology like CORBA is pulled into the runtime of your modern distributed, functional, highly-concurrent wonder code.</p><h4>Writing Modular Code</h4><p>At its simplest modular code in java involves:</p><ul><li>Its name</li><li>The packages a module exposes</li><li>The dependencies of a module</li></ul><p>There are other nuances to consider but this is where a novice would begin to design his library as modules.</p><p>Maven projects (and most other build tools) use modules already to segregate a large codebase into smaller units. This is however only at the codebase level and all the dependencies are still handled by classpaths. With JPMS classpaths become less relevant as the module based jvm parameters like “ — module-path” , “ — module”, etc. have much more control than the traditional classpath</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IV1pRDhcs7TFffHOgs1yPw.png" /></figure><p>In the above project I have defined four modules for my application</p><ul><li><em>com.jtk.nutrition.cli</em>: Module consisting of the main method to run the application in command line</li><li><em>com.jtk.nutrition.ui</em>: Module having the UI component for application (and hence has dependency on the javafx modules)</li><li><em>com.jtk.nutrition.loader</em>: Module for loading the data for the main program</li><li><em>com.jtk.nutrition.sort</em>: Module consisting of various sorting algorithm for the data being loaded</li></ul><p>As you can see the dependency is defined such that the cli and ui components depend on the loader and sorting modules; the ui additionally depends on the javafx modules for the front end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DCzI9-0nWdF07YW-Zk0DiQ.png" /></figure><p>When using maven they are defined as modules, this is as per normal project setup and has nothing to do with JPMS. What makes it different though is the special module-info.java file specified in each of those module source folder as shown below. These files configure the modules for various behaviour</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0EVZ79aI69oE3-os7dqLkg.png" /></figure><ul><li>nutrition-loader/src/main/java/module-info.java</li></ul><pre><strong>module </strong>com.jtk.nutrition.loader{<br>    <strong>requires </strong>java.logging;<br>    <strong>exports </strong>com.jtk.nutrition.loader.model;<br>    <strong>exports </strong>com.jtk.nutrition.loader.util; <br>}</pre><ul><li>nutrition-sort/src/main/java/module-info.java</li></ul><pre><strong>module </strong>com.jtk.nutrition.sort{<br>    <strong>exports </strong>com.jtk.nutrition.sort.util;<br>}</pre><p>The <em>requires </em>keyword indicates the module com.jtk.nutrition.loader requires java.logging module as dependency as it might be calling classes from that module.</p><p>The <em>exports </em>keyword indicates that this module only exposes those packages with in the modules to the other modules. This means that if there was a package com.jtk.nutrition.loader.internal, the classes even though public cannot be called from other modules. This effectively encapsulates the internal code from the client application which previously wasn’t possible.</p><ul><li>nutrition-ui/src/main/java/module-info.java</li></ul><pre><strong>module </strong>com.jtk.nutrition.ui {<br>    <strong>exports </strong>com.jtk.nutrition.ui;<br>    <strong>requires </strong>java.logging; <br>    <strong>requires </strong>javafx.controls; <br>    <strong>requires </strong>javafx.base; <br>    <strong>requires </strong>javafx.graphics;<br><em>    </em><strong><em>requires </em></strong><em>com.jtk.nutrition.loader;<br>    </em><strong><em>requires </em></strong><em>com.jtk.nutrition.sort;</em><br>  }</pre><p>Here the requires keyword makes a dependency to the nutrition-loader and nutrition-sort modules defined above. But it can only access those packages that the module has exposed, i.e. com.jtk.nutrition.loader.model, com.jtk.nutrition.loader.util and com.jtk.nutrition.sort.util.</p><p>Once the modules are designed we should be able to build by running</p><p>mvn clean package</p><p>You could also build the application using command line by running the following for each module</p><pre>javac -d target/classes -p $PATH_TO_FX --module-source-path src $(find . -name ‘*.java’)</pre><p><em>— -module-source-path: Tells the compiler where the source files for the modules are.</em></p><p><em>--module-path</em> (-p)<em>: Tells the compiler/runtime where the compiled modules are that need to be considered for compiling/running code your code</em></p><p>But I found this too tedious and depend on maven to do the hard work.</p><p>And then run using the below command from the project folder (windows)</p><pre>java <strong>-p</strong> nutrition-loader\target\nutrition-loader-1.0-SNAPSHOT.jar;nutrition-ui\target\nutrition-ui-1.0-SNAPSHOT.jar;C:\Users\jubin\apps\Java\javafx-sdk-11.0.2\lib <strong>-m</strong> com.jtk.nutrition.ui/com.jtk.nutrition.ui.Main</pre><p>As you can see the run-time is completely devoid of the class-path references and in its place uses the module based VM parameters</p><ul><li>-p ( —-module-path ) to indicate the jars/classes where the modules are build to; in my case the target folder for the project</li><li>-m (--module) to indicate the main class that needs to run and its associated module: com.jtk.nutrition.ui/com.jtk.nutrition.ui.Main</li></ul><p><em>Observable modules</em>: These are the set of modules that a particular module depends on either by configuration or by default.</p><p>Usually, the below command should be displaying the entire set of observable modules but however it doesn’t seem to work for maven projects.</p><pre>java --list-modules --module-path target/classes</pre><p>As an alternative I right-click on intellij project to generate module diagram, which gives me a visual view of the module dependencies (first diagram above).</p><p>Additionally, I had to add the following dependency in my pom.xml for the ui module to work.</p><pre>&lt;<strong>dependency</strong>&gt;<br>    &lt;<strong>groupId</strong>&gt;org.openjfx&lt;/<strong>groupId</strong>&gt;<br>    &lt;<strong>artifactId</strong>&gt;javafx-controls&lt;/<strong>artifactId</strong>&gt;<br>&lt;/<strong>dependency</strong>&gt;<br>&lt;<strong>dependency</strong>&gt;<br>    &lt;<strong>groupId</strong>&gt;org.openjfx&lt;/<strong>groupId</strong>&gt;<br>    &lt;<strong>artifactId</strong>&gt;javafx-fxml&lt;/<strong>artifactId</strong>&gt;<br>&lt;/<strong>dependency</strong>&gt;</pre><h4>Summary:</h4><p>Essentially we solved two problems so far with the module system. We are able to selectively decide what packages are to be exported from a module there by effectively encapsulating the implementation (i.e. even public classes in the jars are not accessible) and configuring the modules such that each module knows exactly what it depends on without having all the jars in the class-path and hoping for the best.</p><p><a href="https://medium.com/@jubin.kuriakose/java-module-system-part-ii-f051f2110a9e">Java Module System — Part II</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42788ec02268" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>