Java
 Computer >> コンピューター >  >> プログラミング >> Java

Java 9のJShellでドキュメントを参照する方法:/helpコマンドの使い方

Java 9の新機能「JShell」とは

Java 9には、JShellという新しい対話型ツールが導入されました。このツールを使うと、式やクラス、インターフェース、列挙型(enum)などをその場で実行・検証できるため、小さなコード断片(スニペット)の動作確認に非常に便利です。

JShellでは、内部コマンドに関する詳細なドキュメントや、各種オプションの使い方をツール内から直接確認できます。ドキュメントへのアクセスには、次の2つのコマンドを使用します。

  • /help
  • /?

なお、JShellのドキュメントは内部コマンドの情報にとどまらず、Javadocの内容も参照できます。

/helpコマンドで全コマンドの一覧を表示する

以下のコードのように、「/help」コマンドを実行すると、利用可能なすべての内部コマンドとその簡単な説明が一覧形式で表示されます。

jshell> /help
|   Type a Java language expression, statement, or declaration.
|   Or type one of the following commands:
|   /list [<name or id>|-all|-start]
|      list the source you have typed
|   /edit <name or id>
|      edit a source entry referenced by name or id
|   /drop <name or id>
|      delete a source entry referenced by name or id
|   /save [-all|-history|-start] <file>
|      Save snippet source to a file.
|   /open <file>
|      open a file as source input
|   /vars [<name or id>|-all|-start]
|      list the declared variables and their values
|   /methods [<name or id>|-all|-start]
|      list the declared methods and their signatures
|   /types [<names or id>|-all|-start]
|      list the declared types
|   /imports
|      list the imported items
|   /exit
|      exit jshell
|   /env [-class-path <path> ] [-module-path <path>] [-add-modules <modules>] ...
|      view or change the evaluation context
|   /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]...
|      reset jshell
|   /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
|      reset and replay relevant history -- current or previous (-restore)
|   /history
|      history of what you have typed
|   /help [<command>|<subject>]
|      get information about jshell
|   /set editor|start|feedback|mode|prompt|truncation|format ...
|      set jshell configuration information
|   /? [<command>|<subject> ]
|      get information about jshell
|   /!
|      re-run last snippet
|   /
|      re-run snippet by id
|   /-
|      re-run n-th previous snippet
|   For more information type '/help' followed by the name of a command or a subject.
|   For example '/help /list' or '/help intro'.
|
|   Subjects:
|
|   intro
|      an introduction to the jshell tool
|   shortcuts
|      a description of keystrokes for snippet and command completion,
|      information access, and automatic code generation
|   context
|      the evaluation context options for /env /reload and /reset

この出力には、既存の内部コマンドそれぞれの概要が簡潔に表示されます。さらに詳しい説明が必要な場合は、次の方法で個別コマンドの詳細を確認できます。

特定のコマンドの詳細を調べる方法

特定のコマンドについてより詳細な使用方法を知りたい場合は、「/help」の後にコマンド名を続けて入力します。たとえば「/set」コマンドの詳細は、以下のようにして確認できます。

jshell> /help /set
|
|  /set
|
|   Set jshell configuration information, including:
|   the external editor to use, the start-up definitions to use, a new feedback mode,
|   the command prompt, the feedback mode to use, or the format of output.
|
|   /set editor [-wait] ...
|      Specify the command to launch for the /edit command.
|      The <command> is an operating system dependent string.
|
|   /set start <file>
|      The contents of the specified become the default start-up snippet s and commands.
|
|   /set feedback <mode>
|      Set the feedback mode describing displayed feedback for entered snippets and commands.
|
|   /set mode <mode>[<old-mode>] -command|-quiet|-delete
|      Create or update a user-defined feedback mode, optionally copying from an existing mode.
|
|   /set prompt <mode>"<prompt>" "<continuation-prompt>"
|      Set the displayed prompts for a given feedback mode.
|
|   /set truncation <mode> <length> <selector> ...
|      Set the maximum length of a displayed value.
|
|   /set format <mode> <field> "<format>" ...
|      Configure a feedback mode by setting the format of a field when the selector matches.
|
|   /set
|      Show editor, start, and feedback settings as /set commands.
|      To show the settings of any of the above, omit the set value.
|
|      To get more information about one of these forms, use /help with the form specified.
|   For example: /help /set format

/setサブコマンドの設定変更例

JShellの動作環境をカスタマイズしたい場面では、主に以下のような「/set」系コマンドが役立ちます。

  • /set feedback:スニペット実行時のフィードバック表示形式を変更する
  • /set editor:/editコマンドで起動されるデフォルトエディタを変更する
  • /set start:JShell起動時に読み込まれる初期定義を変更する

たとえば「feedback」の詳細を確認するには、次のように入力します。

jshell> /help /set feedback
| Set the feedback mode describing displayed feedback for entered snippets and
commands:
|
| /set feedback [-retain] <mode>
|
| Retain the current feedback mode for future sessions:
|
| /set feedback -retain
|
| Show the feedback mode and list available modes:
|
| /set feedback
|
| Where <mode> is the name of a previously defined feedback mode.
| You may use just enough letters to make it unique.
| User-defined modes can be added, see '/help /set mode'
|
| When the -retain option is used, the setting will be used in this and future
| runs of the jshell tool.
|
| The form without <mode> or -retain displays the current feedback mode and available modes.

このように、JShellでは「/help」コマンドひとつで、コマンド全体の一覧から個別コマンドの詳細な仕様まで、必要なドキュメントをその場ですぐに参照できます。日常的な開発や学習の中で積極的に活用すると、JShellをより効率的に使いこなせるようになるでしょう。

  1. Java 9のJShellでシステムプロパティを取得する方法【コード例付き】

    JShellは、REPL(Read-Evaluate-Print-Loop)と呼ばれる対話型ツールです。main()メソッドを持つクラスを作成しなくても、簡単なJavaの文を入力するとその場で評価され、結果がすぐに表示されます。起動も簡単で、コマンドラインプロンプトで「jshell」と入力するだけでOKです。 システムプロパティを取得するには、System.getProperty()メソッドとSystem.getProperties()メソッドを使用します。 まずは、Systemクラスのstaticメソッドであるproperty()を使って、特定のシステムプロパティの値をJShell上で表示

  2. Java 9でJavaFXを使ってJShellをプログラムから実装する方法

    JShellは、サンプル式を対話的に実行できるツールです。通常はコマンドラインから利用しますが、JavaFXアプリケーションの中でプログラム的にJShellを実装することも可能です。その場合、Javaプログラムに以下のパッケージをインポートする必要があります。import jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet;これらのクラスの役割は次のとおりです。JShell:評価エンジンの本体。式や文を評価(eval)するためのAPIを提供します。SnippetEvent:評価結果として