Java Shell (jshell) is an interactive tool for learning and prototyping in Java. It was introduced with Java 9 and since then I use it ocassionally either for some quick prototyping, during presentations or simply to verify new features in the Java language. Since jshell is a command line tool (with basic intellisense) editing files in jshell is not the best expierience. Fortunatelly, jshell allows changing the default editor and set it to the one of your choice, including Visual Studio Code, Atom or Sublime.
My primary and favorite IDE is IntelliJ it is pretty havy for some really basic source code editing and its built-in support for jshell is far from perfect. So why not to set Visual Studio Code as a default jshell editor?
Set editor for the current jshell session
Open jshell in the terminal
Run jshell command
-w option sets waiting for the file to be closed before returning.
Now you can edit the current session by running the
This will open Visual Studio Code (it opens new tab if you have VSC opened). You can edit the file and once you are done with editing save and close the file.
Set editor and retain the setting between jshell sessions
If you want to retain the setting between jshell sessions you need to add
retain flag while setting the editor:
Learn about the tools I use on macOS in this post: macOS: essential tools for (Java) developer]
The solution presented here will also work for Atom or any other editor with the option to wait for the opened file to be closed before returning.
Do you want to switch Java SDKs easily so that you can play with new Java features in jshell? Use SDKMAN! Check this blog post and learn how to get started with SDKMAN!: Manage multiple Java SDKs with SDKMAN! with ease
Published on Java Code Geeks with permission by Rafal Borowiec, partner at our JCG program. See the original article here: Set Visual Studio Code as default editor in jshell
Opinions expressed by Java Code Geeks contributors are their own.