说明
jshell
是 Java Shell 的缩写,它是从 Java 9 版本开始引入的一个交互式命令行工具,允许用户直接在命令行中运行 Java 代码片段,无需编写和编译完整的 Java 程序。使用 jshell
,你可以快速测试 Java 代码,探索 API,甚至进行原型设计。
基本示例
以下是一些 jshell
的基本用法:
- 启动 jshell:
只需在命令行中输入jshell
并按回车即可启动。 - 运行简单的代码:
jshell> System.out.println("Hello, jshell!");
Hello, jshell!
- 声明变量:
jshell> int x = 10;
x ==> 10
jshell> x = x + 5;
x ==> 15
- 导入类:
jshell> import java.util.ArrayList;
| created class java.util.ArrayList
- 使用自动补全:
jshell
支持自动补全功能,当你输入部分代码并按下 Tab 键时,它会尝试为你补全。 - 查看历史命令:
使用/history
命令可以查看之前执行过的所有命令。 - 退出 jshell:
使用/exit
命令或简单的exit
命令可以退出jshell
。 - 其他命令和选项:
jshell
提供了许多其他命令和选项,允许你控制其行为,例如设置偏好、查看文档等。你可以通过输入/help
来获取更多信息。
总的来说,jshell
是一个强大的工具,尤其是对于快速测试和学习 Java 语言及其 API 来说。它提供了一个交互式的环境,使得代码开发和测试变得更加容易和直观。
查看帮助
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
| /drop <name or id>
| delete a source entry
| /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 [<name or id>|-all|-start]
| list the type declarations
| /imports
| list the imported items
| /exit [<integer-expression-snippet>]
| exit the jshell tool
| /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 the jshell tool
| /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
| reset and replay relevant history -- current or previous (-restore)
| /history [-all]
| history of what you have typed
| /help [<command>|<subject>]
| get information about using the jshell tool
| /set editor|start|feedback|mode|prompt|truncation|format ...
| set configuration information
| /? [<command>|<subject>]
| get information about using the jshell tool
| /!
| rerun last snippet -- see /help rerun
| /<id>
| rerun snippets by ID or ID range -- see /help rerun
| /-<n>
| rerun n-th previous snippet -- see /help rerun
|
| 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
| keys
| a description of readline-like input editing
| id
| a description of snippet IDs and how use them
| shortcuts
| a description of keystrokes for snippet and command completion,
| information access, and automatic code generation
| context
| a description of the evaluation context options for /env /reload and /reset
| rerun
| a description of ways to re-evaluate previously entered snippets
jshell中查看系统信息
jshell
System.out.println(System.getProperty("java.version"))
System.out.println(System.getProperty("java.vendor"))
System.out.println(System.getProperty("os.version"))
System.out.println(System.getProperty("os.name"))