
{"id":310,"date":"2014-10-26T19:03:37","date_gmt":"2014-10-26T19:03:37","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=310"},"modified":"2014-11-06T19:27:50","modified_gmt":"2014-11-06T19:27:50","slug":"discovering-jigsaw-the-new-major-java-9-feature","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=310","title":{"rendered":"Discovering Jigsaw, the new major java 9 feature."},"content":{"rendered":"<p style=\"color: #555555;\"><span style=\"color: #333333;\">The goal of Project Jigsaw is to design and implement a standard module system for the Java SE Platform, and to apply that system to the Platform itself and to the JDK. It was postponed many times, it&#8217;s planed now for java 9 and until now no announcement from oracle that it&#8217;s postponed again.<\/span><\/p>\n<p style=\"color: #555555;\"><span style=\"color: #333333;\">Here&#8217;s what\u00a0Mark Reinhold\u00a0said about the\u00a0m<\/span>otivations behind the Jigsaw modular system:<\/p>\n<blockquote>\n<p style=\"color: #555555;\">A standard module system for the Java Platform will ease the construction, maintenance, and distribution of large applications, at last allowing developers to escape the \u201cJAR hell\u201d of the brittle and error-prone class-path mechanism.<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<p style=\"color: #555555;\">It sounds very good, but It Isn&#8217;t\u00a0the same motivations as the mature <a href=\"http:\/\/www.osgi.org\/Main\/HomePage\">OSGi<\/a>?\u00a0especially since they are many known java applications using it like Eclipse\u00a0 and\u00a0JDeveloper. Why waiting for many years to provides another module system?<\/p>\n<p style=\"color: #555555;\">A first possible explanation , is that the JRE itself needs to be modular. and in this case a modular system must be managed by the JRE, and not by an external container like OSGi.<\/p>\n<p style=\"color: #555555;\">Jigsaw has two major benefits :<\/p>\n<ul>\n<li><span style=\"color: #555555;\">Modularise the JRE itself.<\/span><\/li>\n<li><span style=\"color: #555555;\">Offer a module system that may be used by other Java libraries and applications.<\/span><\/li>\n<\/ul>\n<p style=\"color: #555555;\"><strong>1 &#8211; Modularise the JRE itself<\/strong><\/p>\n<p style=\"color: #555555;\">The standard Java 8 is structured with jars like all previous Java versions. \u00a0Each jar contains many types, and almost all types are present in the rt.jar,<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2012\/10\/jre7graph1.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-498\" title=\"jre7graph1\" src=\"http:\/\/javadepend.files.wordpress.com\/2012\/10\/jre7graph1.png?w=595\" alt=\"\" \/><\/a><\/p>\n<p style=\"color: #555555;\">The current JRE structure has two major drawbacks:<\/p>\n<ul>\n<li><span style=\"color: #555555;\">The JRE is monolithic which has\u00a0many\u00a0incovenients as <span style=\"color: #333333;\">Mark Reinhold<\/span>\u00a0describe in this <a href=\"https:\/\/blogs.oracle.com\/mr\/entry\/massive_monolithic_jdk\">post<\/a>.\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: #555555;\">Some dependency cycles exist between jars, and many dependency cycles exist between packages as shown in the following <a href=\"http:\/\/www.jarchitect.com\/Doc_Matrix\">DSM<\/a>. The black cells represents a dependency cycle.<\/span><\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/jigsaw1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-332\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/jigsaw1.png\" alt=\"jigsaw1\" width=\"458\" height=\"372\" \/><\/a><\/p>\n<p>The JRE implementing Jigsaw is structured by using modules, and each major features are isolated to a specific module, the structure became cleaner as shown by the following dependency graph.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2012\/10\/jisgaw1.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-483\" title=\"jisgaw1\" src=\"http:\/\/javadepend.files.wordpress.com\/2012\/10\/jisgaw1.png?w=595\" alt=\"\" \/><\/a><\/p>\n<h3 style=\"font-weight: bold; color: #555555;\"><\/h3>\n<h3 style=\"font-weight: bold; color: #555555;\">2- Provides a system to modularise java applications<\/h3>\n<p>A module is a collection of Java types with a name, an optional version number, and a formal description of its relationships to other modules. In addition to Java types a module can include resource files, configuration files, native libraries, and native commands. A module can be cryptographically signed so that its authenticity can be validated.<\/p>\n<p style=\"color: #555555;\">The Java programming language is extended to include module declarations for the purpose of defining modules, their content, and their relationships to other modules. A compilation unit containing a module declaration is, by convention, stored in a file named module-info.java and compiled into a file named module-info.class.<\/p>\n<p style=\"color: #555555;\">And to understand better the module concept, let\u2019s go inside the jndi module from the JRE, and explore its declaration.<\/p>\n<pre style=\"color: #555555;\">module jdk.jndi @ 8-ea {\r\n    requires local jdk.auth.internal@8-ea;\r\n    requires local jdk.base.internal@8-ea;\r\n    requires optional jdk.desktop@8-ea;\r\n    requires jdk.rmi@8-ea;\r\n    requires jdk.tls.internal@8-ea;\r\n    requires optional service javax.naming.ldap.StartTlsResponse;\r\n    provides service sun.net.spi.nameservice.NameServiceDescriptor with sun.net.spi.nameservice.dns.DNSNameServiceDescriptor;\r\n    \/\/ default view exports\r\n    exports com.sun.security.auth.*;\r\n    exports com.sun.security.auth.module.*;\r\n    exports javax.naming.*;\r\n    exports javax.naming.directory.*;\r\n    exports javax.naming.event.*;\r\n    exports javax.naming.ldap.*;\r\n    exports javax.naming.spi.*;\r\n\r\n    view jdk.jndi.internal {\r\n       exports com.sun.jndi.toolkit.url.*;\r\n       exports sun.net.dns.*;\r\n       permits jdk.cosnaming;\r\n       permits jdk.kerberos;\r\n     }\r\n}\r\n<\/pre>\n<p>Let\u2019s discover each module section<\/p>\n<p style=\"color: #555555;\"><strong>requires<\/strong><\/p>\n<p style=\"color: #555555;\">Jndi depends upon other modules, until now nothing new, the jars also depends on other ones, but what\u2019s new is that with Jigsaw approach we specify all the modules required with their versions, and the version specified is the used one, we can also specify a range of versions.<br \/>\nIt\u2019s true that Maven provides also an interesting way to specify dependencies between jars, but with Jigsaw we have more control and flexibility when defining module dependencies.<\/p>\n<p style=\"color: #555555;\">The following dependency graph show that\u00a0jndi uses only modules\u00a0 specified by its requires section.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2012\/10\/jisgaw3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-484\" title=\"jisgaw3\" src=\"http:\/\/javadepend.files.wordpress.com\/2012\/10\/jisgaw3.png?w=595&amp;h=211\" alt=\"\" width=\"595\" height=\"211\" \/><\/a><\/p>\n<p style=\"color: #555555;\"><strong>provides<\/strong><\/p>\n<p style=\"color: #555555;\">A service is a well-known set of interfaces and (usually abstract) classes. A service provider is a specific implementation of a service. The classes in a provider typically implement the interfaces and subclass the classes defined in the service itself.<\/p>\n<p style=\"color: #555555;\">A module can declare that it provides a service and specify the service provider by using the \u201cwith\u201d keyword.<\/p>\n<p style=\"color: #555555;\">For example for jndi we have this line in its module declaration:<\/p>\n<pre style=\"color: #555555;\">provides service sun.net.spi.nameservice.NameServiceDescriptor with sun.net.spi.nameservice.dns.DNSNameServiceDescriptor;\r\n<\/pre>\n<p style=\"color: #555555;\">What specify that it provides the service sun.net.spi.nameservice.NameServiceDescriptor and the implementation is sun.net.spi.nameservice.dns.DNSNameServiceDescriptor.<\/p>\n<p style=\"color: #555555;\"><strong>exports<\/strong><\/p>\n<p style=\"color: #555555;\">An exports clause in a module declaration makes the public types in the package it names available to other modules, so we can with Jigsaw defines boundaries, and not all public types could be used from other modules, we must explicitly specify which types are visible.<\/p>\n<p style=\"color: #555555;\">The jndi module exports many packages, what means that only these exported types could be used from other modules.<\/p>\n<p style=\"color: #555555;\"><strong>view<\/strong><\/p>\n<p style=\"color: #555555;\">In large software systems it is often useful to define multiple views of the same module. One view can be declared for general use by any other module, while another provides access to internal interfaces intended only for use by a select set of closely-related modules.<\/p>\n<p style=\"color: #555555;\">For example with jndi we want that com.sun.jndi.toolkit.url be visible only for cosnaming and kerberos modules, as specified in the module declaration.<\/p>\n<pre style=\"color: #555555;\">    view jdk.jndi.internal {\r\n        exports com.sun.jndi.toolkit.url.*;\r\n        exports sun.net.dns.*;\r\n        permits jdk.cosnaming;\r\n        permits jdk.kerberos;\r\n}\r\n<\/pre>\n<p>This way we have more flexibility to define module boundaries.<\/p>\n<p><strong>Create your first modular application<\/strong><\/p>\n<p>The better way to understand the Jigsaw modularity system is to create a mini modular application and explore the module possibilities. For that you can get the\u00a0<a href=\"https:\/\/jdk9.java.net\/download\/\">JDK9 early acces<\/a>\u00a0and\u00a0Follow the steps from this <a href=\"http:\/\/openjdk.java.net\/projects\/jigsaw\/doc\/quickstart.html\">quick start<\/a>\u00a0web page.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>The current JRE has many problems, it&#8217;s monolithic and contains many dependency cycles, and since 2006 some java experts talk about the necessity to modularise the\u00a0JRE itself, which will be The first major benefit of Jigsaw. However uses it to \u00a0modularise the java libraries and applications is\u00a0<span class=\"b1\" style=\"color: #303030;\">c<\/span><span class=\"b2\" style=\"color: #242424;\">ertai<\/span><span class=\"b3\" style=\"color: #181818;\">nly<\/span><span class=\"b4\" style=\"color: #0c0c0c;\">\u00a0not\u00a0<\/span><span class=\"b3\" style=\"color: #181818;\">going\u00a0<\/span><span class=\"b2\" style=\"color: #242424;\">to be<\/span><span class=\"b1\" style=\"color: #303030;\">\u00a0e<\/span><span style=\"color: #3c3c3c;\">asy to achi<span class=\"b1\" style=\"color: #303030;\">ev<\/span><span class=\"b2\" style=\"color: #242424;\">e. Indeed the OSGi is already adopted by many applications, it exist since 2000 it&#8217;s mature and\u00a0\u00a0provides many interesting modularity features. \u00a0And refactoring the existing application is not an easy task.<\/span><\/span><\/p>\n<p><span style=\"color: #3c3c3c;\"><span class=\"b2\" style=\"color: #242424;\">But if the known IDEs provides out of the box some wizards to create easily modular applications, it could encourage the new application developers to use the Jigsaw modularity system.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The goal of Project Jigsaw is to design and implement a standard module system for the Java SE Platform, and to apply that system to the Platform itself and to the JDK. It was postponed many times, it&#8217;s planed now for java 9 and until now no announcement from oracle that it&#8217;s postponed again. Here&#8217;s &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=310\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Discovering Jigsaw, the new major java 9 feature.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=310"}],"version-history":[{"count":29,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":357,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions\/357"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}