Computer >> 컴퓨터 >  >> 프로그래밍 >> Java

Java 9 JShell에서 문서를 확인하는 방법 총정리

Java 9의 새로운 대화형 도구, JShell이란?

Java 9부터 JShell이라는 새로운 대화형(인터랙티브) 도구가 도입되었습니다. 이 도구를 사용하면 표현식(expression), 클래스, 인터페이스, 열거형(enum) 등을 즉시 실행하고 결과를 바로 확인할 수 있어 학습과 테스트에 매우 유용합니다.

JShell은 자체적으로 상세한 문서 기능을 제공합니다. 내부 명령어들의 사용법과 다양한 옵션에 대한 전체 정보를 JShell 안에서 직접 확인할 수 있는데요. 이 문서는 두 가지 명령어를 통해 접근할 수 있습니다.

  • /help
  • /?

JShell의 문서가 제공하는 정보는 단순히 내부 명령어에 국한되지 않습니다. Javadoc 정보까지 함께 포함하고 있어, API 사용법도 손쉽게 조회할 수 있습니다.

/help 명령어로 전체 명령 목록 확인하기

아래 코드 스니펫처럼 /help 명령어를 입력하면 JShell에서 사용할 수 있는 모든 내부 명령어와 간단한 설명을 한눈에 볼 수 있습니다.

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

위 출력 결과에서 볼 수 있듯이, JShell은 기존에 존재하는 내부 명령어들을 각각의 기능과 함께 간결하게 보여줍니다. 그리고 특정 명령어에 대해 더 자세한 설명이 필요하다면, /help 뒤에 해당 명령어 이름을 붙여 입력하기만 하면 됩니다.

/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 feedback — 스니펫 입력 후 표시되는 피드백 방식 변경
  • /set editor — 기본 외부 에디터 변경
  • /set start — JShell 시작 시 로드되는 초기 정의 변경

각 옵션 역시 /help로 세부 정보를 조회할 수 있습니다. 아래는 /help /set 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.

정리

Java 9의 JShell에서 문서를 확인하는 방법은 매우 간단합니다. /help 또는 /? 명령어로 전체 명령 목록을 확인하고, 더 자세한 설명이 필요하면 /help [명령어] 형태로 입력하면 됩니다. 또한 /help intro, /help shortcuts, /help context 같은 주제(subject) 단위 조회도 가능하므로, JShell을 처음 접하는 개발자라면 이 문서 기능을 적극적으로 활용하는 것이 좋습니다.