{"id":301,"date":"2023-04-24T15:26:12","date_gmt":"2023-04-24T15:26:12","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=301"},"modified":"2023-08-23T16:21:57","modified_gmt":"2023-08-23T16:21:57","slug":"jshell-advanced-usage-customization-java-repl","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/","title":{"rendered":"JShell: Advanced Usage and Customization of Java REPL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Java development, JShell has become an indispensable tool for developers looking to streamline their coding process. Introduced in Java 9 as the official Java REPL (Read-Eval-Print Loop), JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts on-the-fly without the need for a full-fledged project setup. This powerful feature significantly improves the efficiency and productivity of Java developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article is intended for experienced Java developers who are already familiar with the basics of JShell and want to explore its advanced usage and customization options. By the end of this article, you&#8217;ll not only gain a deeper understanding of JShell&#8217;s capabilities but also learn how to tailor it to your specific needs, ultimately elevating your Java development experience. Let&#8217;s dive into the world of JShell and unveil its advanced features, customizations, and best practices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">JShell Features<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JShell is packed with a variety of features that make it a powerful tool for Java developers. In this section, we&#8217;ll cover some of the most useful features that contribute to JShell&#8217;s ease of use and efficiency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Tab completion and code suggestions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the standout features of JShell is its tab completion and code suggestions. This functionality allows you to type partial code fragments, press the &#8216;Tab&#8217; key, and JShell will automatically suggest potential completions. This feature not only saves time but also helps avoid typing errors. Additionally, JShell&#8217;s code suggestions include relevant methods, variables, and class names from the current context, making it easier to discover and use available APIs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">History and navigation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell keeps track of your command history, allowing you to easily navigate through previously executed code snippets and commands using the up and down arrow keys. This feature is particularly useful for quickly re-executing or modifying previously entered code. You can also use the &#8216;\/history&#8217; command to display a complete list of your input history, which can be helpful for reviewing past actions or exporting your work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Command-line options and editor integration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell provides various command-line options that enable you to customize its behavior and tailor it to your preferences. Some of these options include setting the initial classpath, enabling or disabling feedback, and launching JShell with a specific JDK version. Moreover, JShell can be integrated with popular Java Integrated Development Environments (IDEs) like IntelliJ IDEA and Eclipse, allowing you to use JShell&#8217;s interactive features directly within your favorite development environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Error reporting and recovery<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell is designed to handle errors gracefully and provide meaningful feedback to help you quickly identify and fix issues. When you encounter a syntax error or runtime exception, JShell displays a detailed error message with relevant information to help you diagnose the problem. Additionally, JShell&#8217;s error recovery mechanism allows you to fix the problematic code snippet and automatically updates any dependent code snippets, saving you the trouble of manually correcting all related errors. This feature is particularly useful in an interactive environment where code snippets are often built incrementally and depend on each other.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced JShell Usage<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we&#8217;ll delve into more advanced JShell features and explore how you can use them to further enhance your interactive programming experience.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Methods and lambda expressions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell allows you to define methods and lambda expressions, enabling you to create reusable code snippets and modularize your interactive code. You can define methods in JShell just like you would in a regular Java source file. Here&#8217;s an example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">sum<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span> a, <span class=\"hljs-keyword\">int<\/span> b)<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> a + b; }\n|  <span class=\"hljs-function\">created method <span class=\"hljs-title\">sum<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span>,<span class=\"hljs-keyword\">int<\/span>)<\/span>\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Similarly, you can create lambda expressions to represent anonymous functions:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; Function&lt;Integer, Integer&gt; square = x -&gt; x * x;\n|  created variable square : Function&lt;Integer, Integer&gt; = $Lambda$<span class=\"hljs-number\">16<\/span>\/<span class=\"hljs-number\">0x000000080012bc40<\/span>@<span class=\"hljs-number\">3f<\/span>8e7a01<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Working with imports and classes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell automatically imports several commonly used Java packages, such as java.io, java.util, and java.math. However, you can also import additional packages or specific classes as needed:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-keyword\">import<\/span> java.time.LocalDateTime;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You can also define your own classes within JShell, allowing you to create more complex and structured code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span> <\/span>{\n   ...&gt;     <span class=\"hljs-keyword\">private<\/span> String name;\n   ...&gt;     <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">int<\/span> age;\n   ...&gt;     \n   ...&gt;     Person(String name, <span class=\"hljs-keyword\">int<\/span> age) {\n   ...&gt;         <span class=\"hljs-keyword\">this<\/span>.name = name;\n   ...&gt;         <span class=\"hljs-keyword\">this<\/span>.age = age;\n   ...&gt;     }\n   ...&gt;     \n   ...&gt;     <span class=\"hljs-function\">String <span class=\"hljs-title\">getName<\/span><span class=\"hljs-params\">()<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> name; }\n   ...&gt;     <span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">getAge<\/span><span class=\"hljs-params\">()<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> age; }\n   ...&gt; }\n|  created <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Evaluating expressions and statements<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell can evaluate both expressions and statements, allowing you to perform calculations, create variables, or execute methods on the fly. Here are some examples:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">jshell&gt; int result = sum(5, 3);\n|  created variable result : int = 8\n\njshell&gt; square.apply(4);\n|  Expression value is: 16\n|    assigned to temporary variable $1 of type Integer<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Managing code snippets and their dependencies<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As you work with JShell, you may end up with numerous code snippets, some of which depend on others. JShell provides several built-in commands to help you manage these snippets and their dependencies:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>\/list<\/strong><\/code>: Lists all code snippets in the current session.<\/li>\n\n\n\n<li><code><strong>\/edit &lt;id&gt;<\/strong><\/code>: Opens an editor to modify a specific code snippet.<\/li>\n\n\n\n<li><code><strong>\/drop &lt;id&gt;<\/strong><\/code>: Removes a code snippet and its dependencies.<\/li>\n\n\n\n<li><code><strong>\/save &lt;filename&gt;<\/strong><\/code>: Saves your code snippets to a file.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When you modify or remove a code snippet, JShell automatically updates any dependent snippets, ensuring that your code remains consistent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, suppose you have two snippets that depend on each other:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-function\">String <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(String name)<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"Hello, \"<\/span> + name + <span class=\"hljs-string\">\"!\"<\/span>; }\n|  <span class=\"hljs-function\">created method <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(String)<\/span>\n\njshell&gt; String greeting <\/span>= greet(<span class=\"hljs-string\">\"John\"<\/span>);\n|  created variable greeting : String = <span class=\"hljs-string\">\"Hello, John!\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">If you modify the <code><strong>greet<\/strong><\/code> method, JShell will automatically update the <code><strong>greeting<\/strong><\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-function\">String <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(String name)<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"Hi, \"<\/span> + name + <span class=\"hljs-string\">\"!\"<\/span>; }\n|  <span class=\"hljs-function\">replaced method <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(String)<\/span>\n|    update modified variable greeting : String <\/span>= <span class=\"hljs-string\">\"Hi, John!\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Customization and Configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JShell offers several customization and configuration options to help you create a tailored interactive programming environment. In this section, we&#8217;ll explore some of these options and how they can be used to enhance your JShell experience.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">JShell startup configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can configure JShell to load custom settings or code snippets automatically when it starts up. To do this, create a startup script file containing JShell commands, code snippets, or imports, and then pass the file to JShell using the <code><strong>--startup<\/strong><\/code> command-line option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell --startup my_startup.jsh<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">For example, you might create a <code><strong>my_startup.jsh<\/strong><\/code> file with the following contents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> java.time.LocalDateTime;\n<span class=\"hljs-keyword\">import<\/span> java.util.stream.Collectors;\n\nSystem.out.println(<span class=\"hljs-string\">\"Welcome to JShell!\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Customizing feedback mode<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell provides several built-in feedback modes that control the amount and format of the information displayed when you execute code snippets. You can switch between these modes using the <code><strong>\/set<\/strong><\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; \/set feedback &#91;verbose|normal|concise|silent]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Alternatively, you can create a custom feedback mode by modifying an existing mode or defining a new one. To do this, use the <code><strong>\/set<\/strong><\/code> command with a <code><strong>mode<\/strong><\/code> parameter:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; \/set mode my_custom_mode normal\njshell&gt; \/set prompt my_custom_mode <span class=\"hljs-string\">\"\\u001B&#91;32mjs&gt; \\u001B&#91;0m\"<\/span>\njshell&gt; \/set feedback my_custom_mode<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This example creates a custom mode based on the <code><strong>normal<\/strong><\/code> mode, but with a green-colored prompt.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining custom commands and aliases<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell allows you to define custom commands and aliases using the <code><strong>\/alias<\/strong><\/code> command. This can be useful for creating shortcuts to frequently used actions or adding new functionality to JShell. Here&#8217;s an example of defining a custom alias:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; \/alias now \/eval LocalDateTime.now()\njshell&gt; \/now\n|  Expression value is: <span class=\"hljs-number\">2023<\/span>-<span class=\"hljs-number\">04<\/span>-<span class=\"hljs-number\">17<\/span>T18:<span class=\"hljs-number\">31<\/span>:<span class=\"hljs-number\">40.817697<\/span>\n|    assigned to temporary variable $<span class=\"hljs-number\">1<\/span> of type LocalDateTime<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we created a custom alias <code>now<\/code> that evaluates the current date and time using the <code><strong>LocalDateTime.now()<\/strong><\/code> method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using external libraries and custom classpath<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell allows you to use external libraries and custom classpath settings to extend its functionality. To use an external library, download the JAR file and add it to JShell&#8217;s classpath using the <code><strong>--class-path<\/strong><\/code> command-line option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell --<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>-<span class=\"hljs-title\">path<\/span> <span class=\"hljs-title\">path<\/span>\/<span class=\"hljs-title\">to<\/span>\/<span class=\"hljs-title\">your<\/span>\/<span class=\"hljs-title\">library<\/span>.<span class=\"hljs-title\">jar<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Alternatively, you can add the JAR file to the classpath during an active JShell session using the <code><strong>\/env<\/strong><\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; \/env --<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>-<span class=\"hljs-title\">path<\/span> <span class=\"hljs-title\">path<\/span>\/<span class=\"hljs-title\">to<\/span>\/<span class=\"hljs-title\">your<\/span>\/<span class=\"hljs-title\">library<\/span>.<span class=\"hljs-title\">jar<\/span>\r<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Once the library is added to the classpath, you can import and use its classes and methods just like you would in a regular Java project:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-keyword\">import<\/span> com.example.MyLibraryClass;\r\njshell&gt; MyLibraryClass.doSomething();\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">JShell API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The JShell API provides a programmatic interface for interacting with JShell, allowing you to embed JShell functionality within your Java applications. In this section, we&#8217;ll explore the JShell API and demonstrate how to use it to create custom JShell instances, evaluate code snippets, and implement event handlers and feedback providers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Overview of JShell API and its capabilities<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The JShell API, available in the <code><strong>jdk.jshell<\/strong><\/code> package, allows you to create and manage JShell instances, execute code snippets, and manage the evaluation state programmatically. Some of the key classes in the API include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>JShell<\/strong><\/code>: Represents an instance of JShell.<\/li>\n\n\n\n<li><code><strong>Snippet<\/strong><\/code>: Represents a code snippet, such as a method, variable, or expression.<\/li>\n\n\n\n<li><code><strong>SnippetEvent<\/strong><\/code>: Represents an event related to a code snippet, such as creation, modification, or deletion.<\/li>\n\n\n\n<li><code><strong>ExecutionControl<\/strong><\/code>: Controls the execution of code snippets in a JShell instance.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a custom JShell instance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To create a custom JShell instance, you can use the <code><strong>JShell<\/strong><\/code> class&#8217;s <code><strong>create()<\/strong><\/code> method. This method returns a <code><strong>JShell.Builder<\/strong><\/code> object that you can use to configure the JShell instance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> jdk.jshell.JShell;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">JShellExample<\/span> <\/span>{\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\r\n        JShell jshell = JShell.create();\r\n        <span class=\"hljs-comment\">\/\/ Use the JShell instance<\/span>\r\n    }\r\n}\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Evaluating code snippets programmatically<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To evaluate code snippets programmatically, use the <code><strong>eval()<\/strong><\/code> method of the <code><strong>JShell<\/strong><\/code> class. This method takes a string containing the code snippet and returns a list of <code><strong>SnippetEvent<\/strong><\/code> objects representing the results:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> jdk.jshell.JShell;\r\n<span class=\"hljs-keyword\">import<\/span> jdk.jshell.SnippetEvent;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">JShellExample<\/span> <\/span>{\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\r\n        JShell jshell = JShell.create();\r\n        String code = <span class=\"hljs-string\">\"int x = 5; int y = 3; x + y;\"<\/span>;\r\n        List&lt;SnippetEvent&gt; events = jshell.eval(code);\r\n\r\n        <span class=\"hljs-keyword\">for<\/span> (SnippetEvent event : events) {\r\n            System.out.println(<span class=\"hljs-string\">\"Snippet: \"<\/span> + event.snippet().source());\r\n            System.out.println(<span class=\"hljs-string\">\"Value: \"<\/span> + event.value());\r\n            System.out.println(<span class=\"hljs-string\">\"Status: \"<\/span> + event.status());\r\n            System.out.println(<span class=\"hljs-string\">\"---\"<\/span>);\r\n        }\r\n    }\r\n}\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Implementing a custom event handler and feedback provider<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can customize the behavior of a JShell instance by implementing event handlers and feedback providers. Event handlers are used to respond to events related to code snippets, while feedback providers control the feedback displayed during the evaluation process. To implement a custom event handler, create a class that extends <code><strong>jdk.jshell.spi.ExecutionControl<\/strong><\/code> and override its methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> jdk.jshell.spi.ExecutionControl;\r\n<span class=\"hljs-keyword\">import<\/span> jdk.jshell.spi.ExecutionEnv;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyExecutionControl<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">ExecutionControl<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> ExecutionControl defaultControl;\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">MyExecutionControl<\/span><span class=\"hljs-params\">(ExecutionEnv env)<\/span> <\/span>{\r\n        <span class=\"hljs-keyword\">this<\/span>.defaultControl = ExecutionControl.generateDefault(env);\r\n    }\r\n\r\n    <span class=\"hljs-comment\">\/\/ Override methods to customize behavior<\/span>\r\n}\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">To implement a custom feedback provider, create a class that implements <code><strong>jdk.jshell.spi.ExecutionControlProvider<\/strong><\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">import<\/span> jdk.jshell.spi.ExecutionControlProvider;\r\n<span class=\"hljs-keyword\">import<\/span> jdk.jshell.spi.ExecutionEnv;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyExecutionControlProvider<\/span> <span class=\"hljs-keyword\">implements<\/span> <span class=\"hljs-title\">ExecutionControlProvider<\/span> <\/span>{\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> String <span class=\"hljs-title\">name<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"MyExecutionControlProvider\"<\/span>;\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> ExecutionControl <span class=\"hljs-title\">generate<\/span><span class=\"hljs-params\">(ExecutionEnv env)<\/span> <\/span>{\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> MyExecutionControl(env);\r\n    }\r\n}\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">JShell Use Cases and Practical Examples<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JShell is a versatile tool that can be used in various scenarios to enhance the Java development process. In this section, we&#8217;ll discuss some practical use cases for JShell and provide examples to demonstrate its capabilities.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Rapid prototyping and experimentation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell is ideal for quickly testing ideas, experimenting with new APIs, and prototyping code snippets without the overhead of setting up a complete Java project. You can use JShell to test individual methods, classes, or even full algorithms to determine their correctness and efficiency.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, suppose you want to test the performance of a sorting algorithm:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-keyword\">import<\/span> java.util.Arrays;\r\n\r\njshell&gt; <span class=\"hljs-keyword\">int<\/span>&#91;] data = {<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">7<\/span>};\r\n\r\njshell&gt; <span class=\"hljs-keyword\">long<\/span> start = System.nanoTime();\r\njshell&gt; Arrays.sort(data);\r\njshell&gt; <span class=\"hljs-keyword\">long<\/span> end = System.nanoTime();\r\n\r\njshell&gt; System.out.println(<span class=\"hljs-string\">\"Sorting time: \"<\/span> + (end - start) + <span class=\"hljs-string\">\" ns\"<\/span>);\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Debugging and diagnosing issues<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell can be used to reproduce and diagnose issues or bugs in your code. By isolating problematic code snippets in JShell, you can quickly identify the cause of the issue and test potential solutions. This can be particularly helpful for debugging complex applications with multiple dependencies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, suppose you encounter a NullPointerException when calling a method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-function\">String <span class=\"hljs-title\">formatName<\/span><span class=\"hljs-params\">(String firstName, String lastName)<\/span> <\/span>{\r\n   ...&gt;     <span class=\"hljs-keyword\">return<\/span> firstName.trim() + <span class=\"hljs-string\">\" \"<\/span> + lastName.trim();\r\n   ...&gt; }\r\n|  <span class=\"hljs-function\">created method <span class=\"hljs-title\">formatName<\/span><span class=\"hljs-params\">(String,String)<\/span>\r\n\r\njshell&gt; String result <\/span>= formatName(<span class=\"hljs-keyword\">null<\/span>, <span class=\"hljs-string\">\"Doe\"<\/span>);\r\n|  java.lang.NullPointerException thrown\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You can use JShell to test different approaches for handling null values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; <span class=\"hljs-function\">String <span class=\"hljs-title\">formatName<\/span><span class=\"hljs-params\">(String firstName, String lastName)<\/span> <\/span>{\r\n   ...&gt;     <span class=\"hljs-keyword\">return<\/span> (firstName == <span class=\"hljs-keyword\">null<\/span> ? <span class=\"hljs-string\">\"\"<\/span> : firstName.trim()) + <span class=\"hljs-string\">\" \"<\/span> + (lastName == <span class=\"hljs-keyword\">null<\/span> ? <span class=\"hljs-string\">\"\"<\/span> : lastName.trim());\r\n   ...&gt; }\r\n|  <span class=\"hljs-function\">replaced method <span class=\"hljs-title\">formatName<\/span><span class=\"hljs-params\">(String,String)<\/span>\r\n\r\njshell&gt; String result <\/span>= formatName(<span class=\"hljs-keyword\">null<\/span>, <span class=\"hljs-string\">\"Doe\"<\/span>);\r\n|  created variable result : String = <span class=\"hljs-string\">\" Doe\"<\/span>\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Creating interactive tutorials and documentation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell can be used to create interactive tutorials, demonstrations, and documentation for Java libraries and APIs. By embedding JShell instances in your documentation or tutorial materials, you can provide users with live, executable examples that help illustrate concepts and reinforce learning.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you can create an interactive tutorial for a custom math library:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; \/save tutorial.jsh\r\njshell&gt; \/open tutorial.jsh\r\n\r\njshell&gt; System.out.println(<span class=\"hljs-string\">\"Welcome to the Math Library Tutorial!\"<\/span>);\r\njshell&gt; System.out.println(<span class=\"hljs-string\">\"Example: Calculate the square of a number.\"<\/span>);\r\njshell&gt; <span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">square<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span> x)<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> x * x; }\r\njshell&gt; System.out.println(<span class=\"hljs-string\">\"Square of 4: \"<\/span> + square(<span class=\"hljs-number\">4<\/span>));\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Automating tasks and scripting<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell can be used as a scripting tool for automating tasks and creating custom scripts in Java. By combining JShell with the Java standard library and external libraries, you can create powerful scripts to automate tasks such as file manipulation, data processing, or system administration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you can create a script to process a CSV file and generate a summary report:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; \/env --<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>-<span class=\"hljs-title\">path<\/span> <span class=\"hljs-title\">path<\/span>\/<span class=\"hljs-title\">to<\/span>\/<span class=\"hljs-title\">csv<\/span>-<span class=\"hljs-title\">library<\/span>.<span class=\"hljs-title\">jar<\/span>\r\n<span class=\"hljs-title\">jshell<\/span>&gt; <span class=\"hljs-title\">import<\/span> <span class=\"hljs-title\">java<\/span>.<span class=\"hljs-title\">nio<\/span>.<span class=\"hljs-title\">file<\/span>.*<\/span>;\r\njshell&gt; <span class=\"hljs-keyword\">import<\/span> java.io.*;\r\njshell&gt; <span class=\"hljs-keyword\">import<\/span> com.opencsv.*;\r\n\r\njshell&gt; <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">processCSV<\/span><span class=\"hljs-params\">(String inputFilename, String outputFilename)<\/span> <\/span>{\r\n   ...&gt;     <span class=\"hljs-keyword\">try<\/span> (CSVReader reader = <span class=\"hljs-keyword\">new<\/span> CSVReader(<span class=\"hljs-keyword\">new<\/span> FileReader(inputFilename));\r\n   ...&gt;          BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputFilename))) {\r\n   ...&gt;         String&#91;] nextLine;\r\n   ...&gt;         <span class=\"hljs-keyword\">int<\/span> lineCount = <span class=\"hljs-number\">0<\/span>;\r\n   ...&gt;         <span class=\"hljs-keyword\">while<\/span> ((nextLine = reader.readNext()) != <span class=\"hljs-keyword\">null<\/span>) {\r\n   ...&gt;             lineCount++;\r\n   ...&gt;         }\r\n   ...&gt;         writer.write(<span class=\"hljs-string\">\"Total lines: \"<\/span> + lineCount);\r\n   ...&gt;     } <span class=\"hljs-keyword\">catch<\/span> (IOException e) {\r\n   ...&gt;         e.printStackTrace();\r\n   ...&gt;     }\r\n   ...&gt; }\r\n\r\njshell&gt; processCSV(<span class=\"hljs-string\">\"input.csv\"<\/span>, <span class=\"hljs-string\">\"report.txt\"<\/span>);\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we created a script to read data from a CSV file, count the number of lines, and write the result to a summary report file. The script leverages an external CSV library and the Java standard library for file handling. This showcases JShell&#8217;s capabilities in automating tasks and creating custom scripts in a Java environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tips and Tricks for Efficient JShell Usage<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To make the most of JShell, it&#8217;s essential to know some tips and tricks that can help you use the tool more efficiently. In this section, we&#8217;ll discuss some useful techniques for enhancing your JShell experience.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Keyboard shortcuts and navigation tips<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell provides a variety of keyboard shortcuts and navigation tips to help you work more efficiently. Some of the most useful shortcuts include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tab: Autocomplete a command or code snippet.<\/li>\n\n\n\n<li>Shift + Tab: Display a list of possible completions.<\/li>\n\n\n\n<li>Up\/Down Arrow: Navigate through the command history.<\/li>\n\n\n\n<li>Ctrl + C: Cancel the current command or code snippet.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, you can use the <code><strong>\/history<\/strong><\/code> command to view your command history and the <code><strong>\/list<\/strong><\/code> command to view the code snippets you&#8217;ve entered during the current session.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code snippet organization and management<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell provides several commands for organizing and managing your code snippets:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>\/list<\/strong><\/code>: List all code snippets entered during the current session.<\/li>\n\n\n\n<li><code><strong>\/save &lt;filename><\/strong><\/code>: Save all code snippets to a file.<\/li>\n\n\n\n<li><code><strong>\/open &lt;filename><\/strong><\/code>: Load code snippets from a file.<\/li>\n\n\n\n<li><code><strong>\/drop &lt;snippet-id><\/strong><\/code>: Remove a specific code snippet.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">By using these commands, you can easily manage your code snippets, save them for future use, and load them as needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Leveraging Java language features in JShell<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JShell supports most Java language features, which means you can use features such as lambdas, streams, and optional values to make your code more concise and expressive:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">jshell&gt; List&lt;String&gt; names = List.of(<span class=\"hljs-string\">\"Alice\"<\/span>, <span class=\"hljs-string\">\"Bob\"<\/span>, <span class=\"hljs-string\">\"Charlie\"<\/span>);\r\njshell&gt; names.stream().filter(n -&gt; n.startsWith(<span class=\"hljs-string\">\"A\"<\/span>)).forEach(System.out::println);\r\nAlice\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, we used Java streams and a lambda expression to filter and print the names starting with &#8220;A&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Integrating JShell with popular Java IDEs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Many popular Java IDEs, such as IntelliJ IDEA and Eclipse, offer built-in support or plugins for JShell. By integrating JShell with your favorite IDE, you can take advantage of the IDE&#8217;s advanced features, such as code completion, syntax highlighting, and debugging, while working with JShell.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To integrate JShell with IntelliJ IDEA, follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open IntelliJ IDEA and create or open a Java project.<\/li>\n\n\n\n<li>Go to the &#8220;Tools&#8221; menu, and click on &#8220;JShell Console.&#8221;<\/li>\n\n\n\n<li>A new JShell console will open, allowing you to use JShell directly within the IDE.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">For Eclipse, you can use the &#8220;JShell for Eclipse&#8221; plugin, which can be installed from the Eclipse Marketplace.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These tips and tricks will help you work more efficiently with JShell and make the most of its powerful features.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java development, JShell has become an indispensable tool for developers looking to streamline their coding process. Introduced in Java 9 as the official Java REPL (Read-Eval-Print Loop), JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts on-the-fly without the need for a full-fledged project setup. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[5,4],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","category-java","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JShell: Advanced Usage and Customization of Java REPL<\/title>\n<meta name=\"description\" content=\"JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JShell: Advanced Usage and Customization of Java REPL\" \/>\n<meta property=\"og:description\" content=\"JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-24T15:26:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-23T16:21:57+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"JShell: Advanced Usage and Customization of Java REPL\",\"datePublished\":\"2023-04-24T15:26:12+00:00\",\"dateModified\":\"2023-08-23T16:21:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/\"},\"wordCount\":2121,\"commentCount\":0,\"articleSection\":[\"Java\",\"Programming Languages\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/\",\"name\":\"JShell: Advanced Usage and Customization of Java REPL\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2023-04-24T15:26:12+00:00\",\"dateModified\":\"2023-08-23T16:21:57+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/jshell-advanced-usage-customization-java-repl\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JShell: Advanced Usage and Customization of Java REPL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1784386264\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1784386264\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1784386264\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JShell: Advanced Usage and Customization of Java REPL","description":"JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/","og_locale":"en_US","og_type":"article","og_title":"JShell: Advanced Usage and Customization of Java REPL","og_description":"JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts","og_url":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/","article_published_time":"2023-04-24T15:26:12+00:00","article_modified_time":"2023-08-23T16:21:57+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"JShell: Advanced Usage and Customization of Java REPL","datePublished":"2023-04-24T15:26:12+00:00","dateModified":"2023-08-23T16:21:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/"},"wordCount":2121,"commentCount":0,"articleSection":["Java","Programming Languages"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/","url":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/","name":"JShell: Advanced Usage and Customization of Java REPL","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2023-04-24T15:26:12+00:00","dateModified":"2023-08-23T16:21:57+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"JShell enables interactive programming by allowing developers to execute Java code snippets, evaluate expressions, and test new concepts","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/jshell-advanced-usage-customization-java-repl\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"JShell: Advanced Usage and Customization of Java REPL"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1784386264","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1784386264","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1784386264","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":8,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/301\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}