In this case, a window will open with a simple editor that will help you modify the snippet:

This editor comes in handy to add or modify multi-line statements like classes or methods and it won't be closed until you click on the Exit (saving the changes) or Cancel (discarding the changes) buttons.

However, in the case of implicit variables, notice that instead of modifying the variable, JShell creates another variable with the new value:

You can import other packages, but how does JShell know where to look for custom packages?

Consider the following case:

1
jshell>importcom.mycompany.*

java

What you have to do is add the packages to the JShell classpath with the /env -class-path <path> command (if we already started a JShell session) or the option $jshell --class-path <path> (on the command-line).

You can specify a directory (in this example, it's the current directory):

1
jshell>/env -class-path .

java

Or a list of directories, JARs, or ZIP archives to search for compiled class files (the list must be separated with : on Linux/Mac and ; on Windows). You can find out more by executing /help context.

This way, assuming you have downloaded the Guava JAR in C:\, we can execute the following command:

Conclusion

JShell is an important addition to the Java language. Users can easily and quickly verify things like return types, adjust formatting options, or try out libraries or language features.

Hopefully this guide taught you what you needed to know about how to work with various types of code snippets in JShell, some of its useful commands, and its auto-completion capabilities to explore a class’ members and documentation.

Of course, this guide only scratches the surface. Advanced users should look forward to learning more about feedback modes, custom options (like setting a custom editor), and ways of integrating JShell into your Java programs. A good resource to learn about some of these topics is Robert Field's tutorial on JShell.