――In this article, I will briefly explain how to use Jshell.
--Participating in JJUG CCC the other day --There, Mr. Kishida (@nowokay) was talking about Java 11 in Jshell.
--Jshell beginners --For those who are tired of using Java
--Mac terminal only --It is assumed that docker is included
--Just hit the command below
docker run -it openjdk:11 jshell
Then ...
You can use it like this
jshell> String hoge = "hoge"
hoge ==> "hoge"
――It will tell you the variables you created
jshell> /vars
| String hoge = "hoge"
| List<String> list = []
| String test = "test"
――It will tell you the one that is currently imported
jshell> /imports
| 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.*
--Striking import directly
jshell> import java.util.Scanner
As expected, this is not cool, so ...
--At the timing of use, press Shift + tab at the same time and press i immediately after releasing.
jshell> ZonedDateTime
0: Do nothing
1: import: java.time.ZonedDateTime
Choice:
Imported: java.time.ZonedDateTime
Then it will import like this
jshell> ZonedDateTime now = ZonedDateTime.now()
now ==> 2018-12-21T16:14:20.037591Z[Etc/UTC]
――This is the same as the terminal, and if you press the tab key, it will complete it.
jshell> var lsit = List.
class copyOf( of(
You can use it by pressing tab at the same timing (after hitting) as the timing to have it complemented by the IDE like this
――If you made such a list
jshell> var list = List.of("hoge","fuga","piyo")
list ==> [hoge, fuga, piyo]
――For this guy, if you try to write a new line and start a new line at an appropriate place, it will be executed.
jshell> var result = list.stream().filter(word -> word.startsWith("h"))
result ==> java.util.stream.ReferencePipeline$2@31610302
――It's really sad, so ...
jshell> var result = list.stream().filter(word -> word.startsWith("h")
...> ).collect(Collectors.toList())
result ==> [hoge]
You can start a new line by opening () like this, or by returning with. In the previous line.
/exit
――It may not be known unexpectedly, but you can get out with this
jshell> /exit
| Goodbye
/help
――If you have any problems, please refer to help.
--Jshell Pretty fun --There are many parts that rely on IDE completion, and I didn't care much about import classes, so it seems to be a trigger. ――As you may have noticed, I inserted the Java 11 code on the way, but as you can see, it is very good because you can easily try Java 11 on the terminal without having to bother to install the JDK from Oracle. ――I wondered if there are quite a few options in Jshell as a place to try things you want to check (such as time conversion) that do not need to create a Sample project.
――For the time being, I just played for about an hour, so I haven't enjoyed it so I would like to know if there are any recommended usage scenes or usage commands.
I referred to Mr. Kishida's Qiita article. Learn JDK11 with JShell (2018/12/15 JJUG CCC 2018 Fall)
Recommended Posts