LoginSignup
1

More than 5 years have passed since last update.

Java9が入ってたのでjshellを試した。

Last updated at Posted at 2018-05-02
  • 気にしてなかったんですが、eclipseにjava9がはいってました。
  • 新しい機能自体はよくわかってないんですが、jshellというのが使えるようになったということで試してみました。

  • jshell コマンドで入って /exit で抜けます。

sh-3.2$ pwd
/Applications/Eclipse_4.7.2.app/Contents/java/9/Home/bin
sh-3.2$ ./jshell
|  JShellへようこそ -- バージョン9.0.1
|  概要については、次を入力してください: /help intro

jshell> System.out.println("HELLO WORLD!")
HELLO WORLD!

jshell> List<String> list = new ArrayList<>()
list ==> []

jshell> list.add("HELLO")
$3 ==> true

jshell> list.add("WORLD")
$4 ==> true

jshell> list
list ==> [HELLO, WORLD]

jshell> String.join("-",list)
$6 ==> "HELLO-WORLD"

jshell> /exit
|  終了します

かる〜く使ってみて

  • import java.util.Listとかいらないんですね。import java.util.*くらいは暗黙の宣言なのかな?
  • 簡単に変数の中身が見えるのはいいかも?

pidを見てみます。

sh-3.2$ ./jshell
|  JShellへようこそ -- バージョン9.0.1
|  概要については、次を入力してください: /help intro

jshell> ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|  エラー:
|  シンボルを見つけられません
|    シンボル:   変数 ManagementFactory
|    場所: クラス
|  ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|  ^---------------^

jshell> import java.lang.management.ManagementFactory

jshell> ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
$2 ==> "4449"
  • 一方psでは、下のようになってます。
sh-3.2$ ps
  PID TTY           TIME CMD
 4448 ttys000    0:08.22 ./jshell
 4449 ttys000    0:00.60 /Applications/Eclipse_4.7.2.app/Contents/java/9/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=localhost:64584 jdk.jshell.execution.R

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1