JShell is a Java REPL tool introduced in JDK 9 (for more information, see JEP 222). It enables you to interactively evaluate Java expressions without the overhead of creating an executable class, compiling your code, and so on.
IntelliJ IDEA includes a basic console for working with JShell from inside the IDE, similar to the Groovy Console. You can use the JShell Console to try out code snippets, which can even reference the classes defined in your project.
Open the JShell Console
From the main menu, select.
This opens the JShell Console tab in the editor, where you can type Java code, for example:
Press Ctrl+Enter to run this code in JShell. You should see the following output in the Run tool window:
In order to use the JShell Console, you need to have Java 9 or later available. You can run IntelliJ IDEA on an older runtime, and your project can use an older JDK, but you must select a supported Java version from the JRE list at the top of the JShell Console tab. Or click to specify the path to a valid Java version.
By default, all libraries that your project depends on are available to the JShell Console. You can use the Use classpath of list to select a specific module.
Add your project's class files as a library
If you want to use the classes from your project in a JShell script, you should add them as a library to your project and include this library as a dependency.
Press Ctrl+Alt+Shift+S to open the Project Structure dialog.
Add your project's class files as a library.
On the Libraries page, click , then Java, and specify the output location of your project's class files as a library.
For more information, see Define a project library.
Use the library as a module dependency.
On the Modules page, select the necessary module, and open the Dependencies tab. In the table with the module's dependencies, click , then Library, and select the library with your project's built classes as a dependency for this module.
For more information, see Add a library to module dependencies.
Restart the JShell Console and select the necessary module under Use classpath of.