Editing the source code in our favorite external code editor
We created a new version of the calculateRectanglePerimeter
method. Now, we want to make similar changes to the calculateRectangleArea
method. However, this time, we will take advantage of an editor to make it easier to make changes to the existing code.
Enter the following command in JShell to launch the default JShell Edit Pad editor to edit the source code for the calculateRectangleArea
method:
/edit calculateRectangleArea
JShell will display a dialog box with JShell Edit Pad and the source code for the calculateRectangleArea
method, as shown in the following screenshot:
JShell Edit Pad lacks most of the features we enjoy from code editors and we cannot even consider it a decent code editor. In fact, it just allows us to easily edit the source code without having to copy and paste from the previous listing. We will learn how to configure a better editor later.
Enter the following code in the JShell Edit Pad to overwrite the method named calculateRectangleArea
with a new code that prints the received width and height values and then prints the calculated area with calls to the Sytem.out.printf
method. The changes are highlighted here:
float calculateRectangleArea(float width, float height) { float area = width * height; System.out.printf("Width: %.2f\n", width); System.out.printf("Height: %.2f\n", height); System.out.printf("Area: %.2f\n", area); return area; }
Click on Accept and then click on Exit. JShell will close the JShell Edit Pad and display the next messages indicating it has modified and overwritten the method named calculateRectangleArea
with two arguments of the float
type:
| modified method calculateRectangleArea(float,float) | update overwrote method calculateRectangleArea(float,float)
Enter the following code in JShell to print the results of calling the recently modified calculateRectangleArea
method with width
and height
as the arguments:
calculateRectangleArea(width, height);
After we enter the previous line, JShell will call the method and it will assign the results to a scratch variable whose name starts with $
and continues with a number. The first lines display the output generated by the three calls to System.out.printf
that we added to the method. Finally, JShell displays the scratch variable name, $24
, the value assigned to the variable that indicates the result returned by the method, 3250.125
, and the type for the scratch variable, float
. The next lines show the messages displayed in JShell after we enter the previous expression that called the new version of the method:
Width: 80.25 Height: 40.50 Area: 3250.13 $24 ==> 3250.125 | created scratch variable $24 : float
The good news is that JShell allows us to easily configure any external editor to edit the code snippets. We just need to grab the complete path to the editor we want to use and run a command in JShell to configure the editor we want to launch whenever we use the /edit
command.
For example, in Windows, the default installation path for the popular Sublime Text 3 code editor is C:\Program Files\Sublime Text 3\sublime_text.exe
. If we want to use this editor to edit code snippets in JShell, we must run the /set editor
command followed by the path enclosed in double quotes. We have to make sure that we replace the backslash (\
) with double backslashes (\\
) in the path string. For the previously explained path, we must run the following command:
/set editor "C:\\Program Files\\Sublimet Text 3\\sublime_text.exe"
After we enter the previous command, JShell will display a message indicating to us that the editor was set to the specified path:
| Editor set to: C:\Program Files\Sublime Text 3\sublime_text.exe
After we change the editor, we can enter the following command in JShell to launch the new editor to make changes to the source code for the calculateRectangleArea
method:
/edit calculateRectangleArea
JShell will launch Sublime Text 3 or any other editor that we might have specified and will load a temporary file with the source code for the calculateRectangleArea
method, as shown in the following screenshot:
Tip
If we save the changes, JShell will automatically overwrite the method as we did when we used the default editor: JShell Edit Pad. After we make the necessary edits, we must close the editor to continue running Java code or JShell commands in JShell.
In any of the platforms, JShell will create a temporary file with the .edit
extension. Thus, we can configure our favorite editor to use Java syntax highlighting whenever we open files with the .edit
extension.
In macOS or Linux, paths are different than in Windows, and therefore, the necessary steps are different. For example, in macOS, in order to launch the popular Sublime Text 3 code editor when it is installed in the default path, we must run /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl
.
If we want to use this editor to edit code snippets in JShell, we must run the /set editor
command followed by the complete path enclosed in double quotes. For the previously explained path, we must run the following command:
/set editor "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
After we enter the previous command, JShell will display a message indicating to us that the editor was set to the specified path:
| Editor set to: /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl
After we change the editor, we can enter the following command in JShell to launch the new editor to make changes to the source code for the calculateRectangleArea
method:
/edit calculateRectangleArea
JShell will launch Sublime Text 3 on macOS or any other editor that we might have specified and will load a temporary file with the source code for the calculateRectangleArea
method, as shown in the following screenshot: