{"id":26081,"date":"2017-09-21T10:00:56","date_gmt":"2017-09-21T10:00:56","guid":{"rendered":"https:\/\/blog.jetbrains.com\/idea\/?p=16012"},"modified":"2017-10-10T09:49:19","modified_gmt":"2017-10-10T09:49:19","slug":"java-9-and-intellij-idea","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/ja\/idea\/2017\/09\/java-9-and-intellij-idea","title":{"rendered":"Java 9 and IntelliJ IDEA"},"content":{"rendered":"<p>Java 9 is released today, so let&#8217;s\u00a0do a quick recap of the existing Java 9 support in IntelliJ IDEA, and have a peek at some of the upcoming features in IntelliJ IDEA 2017.3 for Java 9.\u00a0 Java 9 has been\u00a0fully supported in all of the 2017 releases,\u00a0and most of the features we talk about here are available in\u00a0the current stable release of IntelliJ IDEA.<\/p>\n<p><strong>Jigsaw &amp; the JPMS<\/strong><\/p>\n<p>The big feature for Java 9 is, of course, the <a href=\"http:\/\/openjdk.java.net\/projects\/jigsaw\/spec\/\" target=\"_blank\" rel=\"noopener\">Java Platform Module System<\/a>\u00a0(JPMS), which comes under the umbrella <a href=\"http:\/\/openjdk.java.net\/projects\/jigsaw\/\" target=\"_blank\" rel=\"noopener\">Project Jigsaw.<\/a>\u00a0 Jigsaw itself is a much bigger undertaking and includes things like the modularisation of the JDK itself, what&#8217;s interesting to us as developers is the JPMS.<\/p>\n<p>IntelliJ IDEA has supported the Java Platform Module System since\u00a02016.3,\u00a0this support was\u00a0<a href=\"https:\/\/blog.jetbrains.com\/idea\/2017\/03\/support-for-java-9-modules-in-intellij-idea-2017-1\/\">added to and improved in 2017.1<\/a>.<\/p>\n<p>You can easily create a module-info.java file for your IntelliJ IDEA modules:<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-01-new-module-info.png\" rel=\"attachment wp-att-16061\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16061\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-01-new-module-info.png\" alt=\"01-new-module-info\" width=\"1400\" height=\"886\" \/><\/a><\/p>\n<p>This will give you the basic structure of your module\u00a0declaration, including a module name that matches the name of your IntelliJ IDEA module. \u00a0While you&#8217;re working in this file you get code\u00a0highlighting and completion of course.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-02-code-completion-prev.png\" alt=\"\" width=\"1280\" height=\"720\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-02-code-completion.gif\" \/><\/p>\n<p>If you run\u00a0code from a module with a module-info.java file, IntelliJ IDEA will automatically use the -p argument (module path) instead of -classpath.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-03-module-path.png\" rel=\"attachment wp-att-16064\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16064\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-03-module-path.png\" alt=\"03-module-path\" width=\"1400\" height=\"407\" \/><\/a><\/p>\n<p>IntelliJ IDEA also offers\u00a0quick fixes for some of the common problems you&#8217;ll face as you migrate your code to the JPMS, such as the ability to add a &#8220;requires&#8221; directive<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-04-quick-fixes.png\" rel=\"attachment wp-att-16065\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16065\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-04-quick-fixes.png\" alt=\"04-quick-fixes\" width=\"1400\" height=\"252\" \/><\/a><\/p>\n<p>or detection of\u00a0duplicate directives or other errors that can be fixed<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-05-delete-duplicate.png\" rel=\"attachment wp-att-16044\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16044\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-05-delete-duplicate.png\" alt=\"05-delete-duplicate\" width=\"1400\" height=\"234\" \/><\/a><\/p>\n<p>IntelliJ IDEA also lets you create module diagrams to help you visualise the modules you&#8217;re using. \u00a0You can see JDK modules, your\u00a0own modules and automatic modules in different colours, and transitive requirements are highlighted with a stronger line.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16045\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-06-module-diagram-prev.png\" alt=\"\" width=\"1280\" height=\"720\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-06-module-diagram.gif\" \/><\/p>\n<p>These diagrams are particularly useful when you&#8217;re getting started with modularity, as they help you understand which modules you&#8217;re using and where they come from. \u00a0You can even navigate to the modules from the diagram.<\/p>\n<p>IntelliJ IDEA has inspections\u00a0to help migrate code to Java 9. For example, you can\u00a0highlight the use of automatic modules.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-07-automatic-modules.png\" rel=\"attachment wp-att-16047\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16047\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-07-automatic-modules.png\" alt=\"07-automatic-modules\" width=\"1400\" height=\"633\" \/><\/a><\/p>\n<p>This means you can see if you&#8217;re dependent on something that isn&#8217;t a true Java 9 module. This is important as these automatic modules may change over time.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-08-warning-on-automatic-module.png\" rel=\"attachment wp-att-16049\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16049\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-08-warning-on-automatic-module.png\" alt=\"08-warning-on-automatic-module\" width=\"1400\" height=\"227\" \/><\/a><\/p>\n<p>Find out more about\u00a0<a href=\"https:\/\/blog.jetbrains.com\/idea\/2017\/03\/support-for-java-9-modules-in-intellij-idea-2017-1\/\">basic support for modules in IntelliJ IDEA<\/a>, and <a href=\"https:\/\/blog.jetbrains.com\/idea\/2017\/07\/support-for-java-9-in-intellij-idea-2017-2\/\">module diagrams and additional quick fixes<\/a>.<\/p>\n<p><strong>Convenience Factory Methods<\/strong><\/p>\n<p>Java 9 introduces a lot of small API changes that made life easier for developers. One of these is to simplify the creation of Collections with fixed contents. \u00a0IntelliJ IDEA 2017.2 introduced an inspection which can show you where you can use this feature.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-07-immutable-collections.png\" rel=\"attachment wp-att-16048\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16048\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-07-immutable-collections.png\" alt=\"07-immutable-collections\" width=\"1718\" height=\"516\" \/><\/a><\/p>\n<p>This will highlight code that can be simplified by using the Java 9 factory methods<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16050\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-09-using-immutable-collections-prev.png\" alt=\"\" width=\"1280\" height=\"720\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-09-using-immutable-collections.gif\" \/><\/p>\n<p><strong>Thread.onSpinWait<\/strong><\/p>\n<p>Java 9\u00a0lets you give hints to the JVM\u00a0that can help\u00a0performance. \u00a0Specifically, if your code is spinning in a loop waiting for something to happen, you can use <a href=\"http:\/\/download.java.net\/java\/jdk9\/docs\/api\/java\/lang\/Thread.html#onSpinWait--\" target=\"_blank\" rel=\"noopener\">Thread.onSpinWait<\/a> to let the runtime know. IntelliJ IDEA locates code where this hint may be useful, and can add it.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-10-on-spin-wait.png\" rel=\"attachment wp-att-16052\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16052\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-10-on-spin-wait.png\" alt=\"10-on-spin-wait\" width=\"1400\" height=\"231\" \/><\/a><\/p>\n<p>This inspection and the one for Convenience Factory Methods were covered previously in the <a href=\"https:\/\/blog.jetbrains.com\/idea\/2017\/07\/support-for-java-9-in-intellij-idea-2017-2\/\">blog post and video on Java 9 support in IntelliJ IDEA 2017.2<\/a>.<\/p>\n<p><strong>Enhanced Deprecation<\/strong><\/p>\n<p>Another seemingly small feature in Java 9\u00a0that makes it easier for developers to understand the impact of their code is enhanced deprecation. \u00a0Now instead of merely tagging code as deprecated and (possibly) documenting the reasons in the Javadoc,\u00a0the @Deprecated tag supports two useful new attributes, &#8220;since&#8221; and &#8220;forRemoval&#8221;. \u00a0There&#8217;s a new inspection in IntelliJ IDEA 2017.3 that highlights\u00a0any use of code that has been marked &#8220;forRemoval&#8221;.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-11-for-removal-inspection.png\" rel=\"attachment wp-att-16053\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16053\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-11-for-removal-inspection.png\" alt=\"11-for-removal-inspection\" width=\"1400\" height=\"539\" \/><\/a><\/p>\n<p>If you&#8217;ve got this inspection turned on and set to &#8220;Warning&#8221; (which is the default), then when you&#8217;re\u00a0using something that&#8217;s been tagged as &#8220;forRemoval&#8221;, IntelliJ IDEA will strike this method through in red, rather than the usual black.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-12-deprecated.png\" rel=\"attachment wp-att-16054\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16054\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-12-deprecated.png\" alt=\"12-deprecated\" width=\"1400\" height=\"207\" \/><\/a><\/p>\n<p><strong>JShell<\/strong><\/p>\n<p>Java 9 comes with a REPL for Java for the first time, called JShell. \u00a0You can run this REPL from the command line, or from the terminal inside IntelliJ IDEA. \u00a0In addition, IntelliJ IDEA 2017.3 also has a basic console that allows you to work with JShell from inside the IDE, similar to the <a href=\"https:\/\/www.jetbrains.com\/help\/idea\/launching-groovy-interactive-console.html\" target=\"_blank\" rel=\"noopener\">Groovy Console<\/a>.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-13-jshell-menu.png\" rel=\"attachment wp-att-16055\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16055\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-13-jshell-menu.png\" alt=\"13-jshell-menu\" width=\"1400\" height=\"639\" \/><\/a><\/p>\n<p>You can use this console to try out code snippets, and you will be able to select the module path for running this code.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16056\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-14-jshell-prev.png\" alt=\"\" width=\"1280\" height=\"720\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-14-jshell.gif\" \/><\/p>\n<p>This is currently available in the <a href=\"https:\/\/blog.jetbrains.com\/idea\/2017\/09\/whats-new-in-intellij-idea-2017-3-eap\/\">2017.3 EAP<\/a>. \u00a0Please try it out and give us feedback!<\/p>\n<p><strong>Features that don&#8217;t need extra support<\/strong><\/p>\n<p>There are loads of new features in Java 9, including changes to the Streams API, additions to Optional, private methods on interfaces, and more.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-15-streams.png\" rel=\"attachment wp-att-16058\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16058\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-15-streams.png\" alt=\"15-streams\" width=\"1400\" height=\"236\" \/><\/a><\/p>\n<p>Of course these new methods are all available during code completion if your project code level is set to Java 9.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-16-project-language.png\" rel=\"attachment wp-att-16059\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16059\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-16-project-language.png\" alt=\"16-project-language\" width=\"1400\" height=\"731\" \/><\/a><\/p>\n<p>In addition, you can use the usual features of IntelliJ IDEA to take advantage of\u00a0some of the Java 9 features. \u00a0For example, you can generate <a href=\"http:\/\/openjdk.java.net\/jeps\/224\" target=\"_blank\" rel=\"noopener\">HTML 5 Javadoc<\/a> by adding a flag\u00a0when you generate your documentation.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-17-html5.png\" rel=\"attachment wp-att-16060\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16060\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/09\/idea-17-html5.png\" alt=\"17-html5\" width=\"1400\" height=\"874\" \/><\/a><\/p>\n<p>You can even use IntelliJ IDEA to create <a href=\"http:\/\/openjdk.java.net\/jeps\/238\" target=\"_blank\" rel=\"noopener\">Multi-Release\u00a0Jar Files<\/a>. \u00a0This is probably something that&#8217;s best left to build tools like Gradle, Maven or Ant, but it is possible in IntelliJ IDEA, and will be the subject of a later blog post.<\/p>\n<p>All that remains is to say &#8220;Welcome Java 9! \u00a0We&#8217;re ready for you!&#8221;<\/p>\n<p>UPDATE: Join us Tuesday, October 3, at 16:00 &#8211; 17:00 CEST, for our free live webinar, <em>Real World Java 9.<\/em> <a href=\"https:\/\/info.jetbrains.com\/IntelliJ-IDEA-Webinar-October2017.html\" target=\"_blank\" rel=\"noopener\">Register now!<\/a><\/p>\n","protected":false},"author":360,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[826,808,601],"tags":[3140],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea\/26081"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/users\/360"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/comments?post=26081"}],"version-history":[{"count":0,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea\/26081\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/media?parent=26081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/categories?post=26081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/tags?post=26081"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/cross-post-tag?post=26081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}