jshell交互式 Java命令行工具

26 篇文章 0 订阅

说明

jshell 是 Java Shell 的缩写,它是从 Java 9 版本开始引入的一个交互式命令行工具,允许用户直接在命令行中运行 Java 代码片段,无需编写和编译完整的 Java 程序。使用 jshell,你可以快速测试 Java 代码,探索 API,甚至进行原型设计。

基本示例

以下是一些 jshell 的基本用法:

  1. 启动 jshell
    只需在命令行中输入 jshell 并按回车即可启动。
  2. 运行简单的代码
jshell> System.out.println("Hello, jshell!");  
Hello, jshell!
  1. 声明变量
jshell> int x = 10;  
x ==> 10  
jshell> x = x + 5;  
x ==> 15
  1. 导入类
jshell> import java.util.ArrayList;  
|  created class java.util.ArrayList
  1. 使用自动补全
    jshell 支持自动补全功能,当你输入部分代码并按下 Tab 键时,它会尝试为你补全。
  2. 查看历史命令
    使用 /history 命令可以查看之前执行过的所有命令。
  3. 退出 jshell
    使用 /exit 命令或简单的 exit 命令可以退出 jshell
  4. 其他命令和选项
    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"))
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值