1. Introduction
This article is about jshell, an interactive REPL (Read-Evaluate-Print-Loop) console that is bundled with the JDK for the upcoming Java 9 release. For those not familiar with the concept, a REPL allows to interactively run arbitrary snippets of code and evaluate their results.A REPL can be useful for things such as quickly checking the viability of an idea or figuring out e.g. a formatted string for String or SimpleDateFormat.
2. Running
To get started we need to run the REPL, which is done by invoking:
1
| $JAVA_HOME /bin/jshell |
1
| $JAVA_HOME /bin/jshell - v |
3. Defining and Invoking Methods
Methods can be added by typing their signature and body:
1
2
| jshell> void helloWorld() { System.out.println( "Hello world" );} | created method helloWorld() |
1
2
| jshell> helloWorld() Hello world |
4. Variables
Variables can be defined with the normal Java declaration syntax:
1
2
3
4
5
6
7
8
9
10
11
| jshell> int i = 0; i ==> 0 | created variable i : int jshell> String company = "Baeldung" company ==> "Baeldung" | created variable company : String jshell> Date date = new Date() date ==> Sun Feb 26 06:30:16 EST 2017 | created variable date : Date |
1
2
3
| jshell> File file file ==> null | created var iable file : File |
5. Expressions
Any valid Java expression is accepted and the result of the evaluation will be shown. If no explicit receiver of the result is provided, “scratch” variables will be created:
1
2
3
| jshell> String. format ( "%d of bottles of beer" , 100) $6 ==> "100 of bottles of beer" | created scratch variable $6 : String |
Multi-line expressions are also possible. Jshell is smart enough to know when an expression is incomplete and will prompt the user to continue on a new line:
1
2
3
4
5
| jshell> int i = ...> 5; i ==> 5 | modified variable i : int | update overwrote variable i : int |
6. Commands
Jshell provides quite a few meta-commands that aren’t related to evaluating Java statements. They all start with a forward-slash (/) to be distinguished from normal operations. For example, we can request a list of all available commands by issuing /help or /?.Let’s take a look at some of them.
6.1. Imports
To list all the imports active in the current session we can use the /import command:
1
2
3
4
5
6
7
8
9
10
11
| jshell> /import | import java.io.* | import java.math.* | import java.net.* | import java.nio. file .* | import java.util.* | import java.util.concurrent.* | import java.util. function .* | import java.util.prefs.* | import java.util.regex.* | import java.util.stream.* |
6.2. Lists
Working in a REPL is not nearly as easy as having a full-featured IDE at our fingertips: it is easy to forget what variables have which values, what methods have been defined and so on. To check the state of the shell we can use /var, /methods, /list or /history:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| jshell> /var | int i = 0 | String company = "Baeldung" | Date date = Sun Feb 26 06:30:16 EST 2017 | File file = null | String $6 = "100 of bottles of beer on the wall" jshell> /methods | void helloWorld() jshell> /list 1 : void helloWorld() { System.out.println( "Hello world" );} 2 : int i = 0; 3 : String company = "Baeldung" ; 4 : Date date = new Date(); 5 : File file ; 6 : String. format ( "%d of bottles of beer on the wall" , 100) jshell> /history void helloWorld() { System.out.println( "Hello world" );} int i = 0; String company = "Baeldung" Date date = new Date() File file String. format ( "%d of bottles of beer on the wall" , 100) /var /methods /list /history |
6.3. Saving
To save the expression history the /save command can be used:
1
| jshell> /save repl.java |
6.4. Loading
To load a previously saved file we can use the /open command:
1
| jshell> /open repl.java |
6.5. Exiting
When we are done with the work, the /exit command can terminate the shell:
1
2
| jshell> /exit | Goodbye |
Комментариев нет:
Отправить комментарий