Java 9 and IntelliJ IDEA
Java 9 is released today, so let’s do 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. Java 9 has been fully supported in all of the 2017 releases, and most of the features we talk about here are available in the current stable release of IntelliJ IDEA.
Jigsaw & the JPMS
The big feature for Java 9 is, of course, the Java Platform Module System (JPMS), which comes under the umbrella Project Jigsaw. Jigsaw itself is a much bigger undertaking and includes things like the modularisation of the JDK itself, what’s interesting to us as developers is the JPMS.
IntelliJ IDEA has supported the Java Platform Module System since 2016.3, this support was added to and improved in 2017.1.
You can easily create a module-info.java file for your IntelliJ IDEA modules:
This will give you the basic structure of your module declaration, including a module name that matches the name of your IntelliJ IDEA module. While you’re working in this file you get code highlighting and completion of course.
If you run code from a module with a module-info.java file, IntelliJ IDEA will automatically use the -p argument (module path) instead of -classpath.
IntelliJ IDEA also offers quick fixes for some of the common problems you’ll face as you migrate your code to the JPMS, such as the ability to add a “requires” directive
or detection of duplicate directives or other errors that can be fixed
IntelliJ IDEA also lets you create module diagrams to help you visualise the modules you’re using. You can see JDK modules, your own modules and automatic modules in different colours, and transitive requirements are highlighted with a stronger line.
These diagrams are particularly useful when you’re getting started with modularity, as they help you understand which modules you’re using and where they come from. You can even navigate to the modules from the diagram.
IntelliJ IDEA has inspections to help migrate code to Java 9. For example, you can highlight the use of automatic modules.
This means you can see if you’re dependent on something that isn’t a true Java 9 module. This is important as these automatic modules may change over time.
Find out more about basic support for modules in IntelliJ IDEA, and module diagrams and additional quick fixes.
Convenience Factory Methods
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. IntelliJ IDEA 2017.2 introduced an inspection which can show you where you can use this feature.
This will highlight code that can be simplified by using the Java 9 factory methods
Thread.onSpinWait
Java 9 lets you give hints to the JVM that can help performance. Specifically, if your code is spinning in a loop waiting for something to happen, you can use Thread.onSpinWait to let the runtime know. IntelliJ IDEA locates code where this hint may be useful, and can add it.
This inspection and the one for Convenience Factory Methods were covered previously in the blog post and video on Java 9 support in IntelliJ IDEA 2017.2.
Enhanced Deprecation
Another seemingly small feature in Java 9 that makes it easier for developers to understand the impact of their code is enhanced deprecation. Now instead of merely tagging code as deprecated and (possibly) documenting the reasons in the Javadoc, the @Deprecated tag supports two useful new attributes, “since” and “forRemoval”. There’s a new inspection in IntelliJ IDEA 2017.3 that highlights any use of code that has been marked “forRemoval”.
If you’ve got this inspection turned on and set to “Warning” (which is the default), then when you’re using something that’s been tagged as “forRemoval”, IntelliJ IDEA will strike this method through in red, rather than the usual black.
JShell
Java 9 comes with a REPL for Java for the first time, called JShell. You can run this REPL from the command line, or from the terminal inside IntelliJ IDEA. In addition, IntelliJ IDEA 2017.3 also has a basic console that allows you to work with JShell from inside the IDE, similar to the Groovy Console.
You can use this console to try out code snippets, and you will be able to select the module path for running this code.
This is currently available in the 2017.3 EAP. Please try it out and give us feedback!
Features that don’t need extra support
There are loads of new features in Java 9, including changes to the Streams API, additions to Optional, private methods on interfaces, and more.
Of course these new methods are all available during code completion if your project code level is set to Java 9.
In addition, you can use the usual features of IntelliJ IDEA to take advantage of some of the Java 9 features. For example, you can generate HTML 5 Javadoc by adding a flag when you generate your documentation.
You can even use IntelliJ IDEA to create Multi-Release Jar Files. This is probably something that’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.
All that remains is to say “Welcome Java 9! We’re ready for you!”
UPDATE: Join us Tuesday, October 3, at 16:00 – 17:00 CEST, for our free live webinar, Real World Java 9. Register now!
yitian says:
September 22, 2017how soon IDEA 2017.3 will come? I can’t wait any more
Leonardo Otto says:
September 22, 2017Can i boot IntelliJ over JDK 9?
Trisha Gee says:
September 27, 2017Do you mean can you run IntelliJ IDEA on Java 9? Usually IntelliJ runs on its own JDK these days (which is a Java 8 JDK), but you can still use JDK 9 to compile and run your own projects.
Paul says:
September 29, 2017Nope, for now, there are some Exceptions with using JDK 9 to boot IDE
RAM KUMAR MADDELA says:
June 6, 2018i am facing the same issue here, not able to add module-info.java to the project
RAM K MADDELA says:
June 6, 2018got it… fixed thanks
IGOR GANAPOLSKY says:
September 27, 2017Does Kotlin work with JDK 9 in IntelliJ?
Trisha Gee says:
September 29, 2017Yes it does.
Robyn Liu says:
September 29, 2017Pretty Cool!
Andrew says:
September 30, 2017Is it possible to use JShell with Kotlin?
Trisha Gee says:
October 2, 2017Do you mean, can you write Kotlin in JShell? I haven’t tried, but I don’t think it would work. It wouldn’t work on the command line since JShell is a command from the JVM, and I don’t think it would work in the IntelliJ JShell Console (right now, anyway) as it just delegates to JShell.
Nick Radonic says:
October 6, 2017Any manuals on the jshell tools?
Trisha Gee says:
October 10, 2017There’s a very comprehensive tutorial on the Oracle JDK JShell tool.
I don’t think we’ve published the documentation for the JShell console in IntelliJ IDEA yet since it’s part of the EAP and not the supported release.
Markus S says:
October 7, 2017Will the jshell replace the “Evaluate Expression” popup in the debugger tab?
Trisha Gee says:
October 10, 2017There are no plans for that at the moment. What sort of features/advantages do you think that would bring?
Markus S says:
October 11, 2017I have not used jshell much so I cannot say. The evaluate expression has everything I need (and I need it a lot)
ak says:
October 9, 2017It looks like IntelliJ IDEA does not recognise openjdk 9 directory structure.
I built openjdk 9 form github https://github.com/dmlloyd/openjdk,
then selected this newly built jdk as a project jdk in a new IntellijIDEA project,
and is says that the selected directory is not valid home for jdk
The jdk itself looks fine
~/openjdk/build/linux-x86_64-normal-server-release/jdk$ ls -la
total 32
drwxrwxr-x 7 ak ak 4096 окт. 6 02:42 .
drwxrwxr-x 11 ak ak 4096 окт. 7 05:07 ..
drwxrwxr-x 2 ak ak 4096 окт. 6 02:41 bin
drwxrwxr-x 4 ak ak 4096 окт. 6 02:23 conf
drwxrwxr-x 3 ak ak 4096 окт. 6 02:23 include
drwxrwxr-x 5 ak ak 4096 окт. 6 02:42 lib
drwxrwxr-x 77 ak ak 4096 окт. 6 02:33 modules
-rw-rw-r– 1 ak ak 0 окт. 6 02:42 _packages_attribute.done
-rw-rw-r– 1 ak ak 61 окт. 6 02:23 release
Is there some workaround to make idea start using the selected jdk?
Dmitry Batrak says:
October 10, 2017Use ~/openjdk/build/linux-x86_64-normal-server-release/images/jdk-bundle/jdk-9.jdk instead.
Dmitry Batrak says:
October 10, 2017Sorry, just realized it’s Linux. Path can probably be a bit different, but look in ‘images’ folder anyway.
Timur Yuldashev says:
October 10, 2017Seems the problem described in the case: IDEA-178743 Support for development builds of JDK – https://youtrack.jetbrains.com/issue/IDEA-178743
ak says:
October 10, 2017yep, I see now
the jdk path on linux is ~/openjdk/build/linux-x86_64-normal-server-release/images/jdk
Thanks
symon says:
October 19, 2017What’s wrong that it show ‘Module not find: java.base’ when i want to require java.base module in module-info.java in intellij idea
I have installed the jdk9 and set it in idea.
Trisha Gee says:
October 19, 2017That should work (although you don’t need to require java.base, you get that for “free” without declaring it). Can you give us more details about how you’ve set up the project? Or point us to some of the code?
symon says:
October 21, 2017In project, I firstly set up the SDKs, add java9 jdk.(File -> Project Structure ->SDKs)
Then set up ‘Project’ SDK with java9 (File -> Project Structure -> Project).
After setting that in my project, I could not find any Java Class which original under
rt.jar before java9 release, e.g. ArrayList, HashMap…
Have I missed some important settings in project ?
symon says:
October 21, 2017In project, I firstly set up the SDKs, add java9 jdk.(File -> Project Structure ->SDKs)
Then set up ‘Project’ SDK with java9 (File -> Project Structure -> Project).
After setting that in my project, I could not find any Java Class which original under
rt.jar before java9 release, e.g. ArrayList, HashMap…
Have I missed some important settings in project ?
Will Dee says:
December 6, 2017I am moving and existing project to Java 9 for compilation purposes only. I cannot make a module from it (no module.java allowed).
How can I inform the IDE that jam modules are required? I use –add-module java.se.ee when running a JVM, how do I make my project aware of that dependency?
This is a gradle project, and those directives are in the compile tasks.
Roman Shevchenko says:
December 7, 2017At the moment, you need to specify these options explicitly, both for the compiler (Settings | Build, Execution, Deployment | Compiler | Java Compiler) and in run configurations. We’re thinking about adding some UI for this.
Emre Besirik says:
December 27, 2017When will Intellij start to bundle Java 9 instead of Java 8?
Tad Yeager says:
January 11, 2018How do you debug a Java 9 app? You cannot start the IDE with Java 9; when I try to debug something compiled with Java 9, the Java 8 loader kicks in and says that class version is wrong.
What’s meant by “Java 9 has been fully supported in all of the 2017 releases” at the top of this post?
5 Favorite Features of Java 9 – Detroit Labs says:
December 9, 2018[…] Java 9 and IntelliJ IDEA […]