java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java交互式解释器JShell

Java9新特性之命令行交互式解释器JShell全解析

作者:morris131

JShell是Java 9引入的交互式解释器,允许开发者在命令行中即时编译和运行Java代码片段,无需编写完整的类结构,它支持自动导入、变量和方法保留、错误处理等功能,本文给大家介绍Java9新特性之命令行交互式解释器JShell,感兴趣的朋友跟随小编一起看看吧

REPL,全称Read Eval Print Loop,中文是交互式解释器,其实,就是一种代码所见即所得的即时编译器。

Java 9引入了REPL,并将其命令为JShell,这真是我们Java开发的福音,以后演示代码的时候再也不用搬着一个IDE到处跑了。

对于我们Java开发者来说,应该是Java 9带来的最大的个性吧。我们终于可以像Python、Ruby和Node.js那样在Shell可见即可得的运行一些范例代码了。

也就是说,使用REPL,我们可以编写和测试基于Java的逻辑,无需使用javac进行编译,直接查看计算结果。

JShell的主要特性

JShell的使用

要使用JShell,只需在命令行中输入jshell并按回车键。然后,你就可以开始输入Java代码片段并查看结果了。

$ jshell
Feb 26, 2024 2:30:09 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 18.0.1.1
|  For an introduction type: /help intro

查询帮助

正如提示那样,我们可以输入/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
|  /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

注意:千万不要省略开头的反斜杠/

如果要查看某个具体的命令的帮助信息,可以输入/help [command]来获得。

比如/help intro获取工具简介:

jshell> /help intro
|
|                                   intro
|                                   =====
|
|  The jshell tool allows you to execute Java code, getting immediate results.
|  You can enter a Java definition (variable, method, class, etc), like:  int x = 8
|  or a Java expression, like:  x + x
|  or a Java statement or import.
|  These little chunks of Java code are called 'snippets'.
|
|  There are also the jshell tool commands that allow you to understand and
|  control what you are doing, like:  /list
|
|  For a list of commands: /help

比如/help /list查询/list命令的使用:

jshell> /help /list
|
|                                   /list
|                                   =====
|
|  Show the snippets, prefaced with their snippet IDs.
|
|  /list
|       List the currently active snippets of code that you typed or read with /open
|
|  /list -start
|       List the evaluated startup snippets
|
|  /list -all
|       List all snippets including failed, overwritten, dropped, and startup
|
|  /list <name>
|       List snippets with the specified name (preference for active snippets)
|
|  /list <id>
|       List the snippet with the specified snippet ID.
|       One or more IDs or ID ranges may used, see '/help id'

查看JShell默认导入哪些包

我们可以使用/imports命令查看JShell默认导入了哪些包:

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

使用JShell import命令导入某个包或文件

我们可以使用import命令导入某个类或包,就像我们代码中使用的那样:

jshell> import java.time.*
jshell> /import
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*
|    import java.time.*

导入外部jar

我们可以使用/env命令将第三方的jar添加到class-path中,这样就可以使用import命令导入第三方的类了:

jshell> /env -class-path /root/hutool-all-5.8.12.jar
|  Setting new options and restoring state.
jshell> import cn.hutool.core.util.*
jshell> StrUtil.isBlankIfStr("abc")
$2 ==> false

使用JShell进行一些简单的数学运算

JShell默认支持一些简单的数学运算符,例如:

jshell> 5 * 6
$2 ==> 30
jshell> $2
$2 ==> 30
jshell> 10 * $2
$4 ==> 300
jshell> int a = $4
a ==> 300
jshell> a
a ==> 300

可以看到,JShell会将每一此执行的结果保存到一个以$开始的变量中,而后面,我们就可以对这些变量进行引用。

JShell中默认的上下文

可以在JShell中使用我们平时在Java中使用的类和方法来获取当前的运行上下文。

获取当前的执行线程:

jshell> Thread.currentThread()
$7 ==> Thread[main,5,main]

在JShell中定义一些方法

因为内部方法和内部类的关系,我们还可以在JShell中定义一个方法。

在一行内定义:

jshell> int add(int a, int b) { return a + b;}
|  created method add(int,int)
jshell> add(3,4)
$10 ==> 7

在多行定义:

jshell> int sub(int a, int b) {
   ...>     return a - b;
   ...> }
|  created method sub(int,int)
jshell> sub(10, 5)
$12 ==> 5

退出JShell

如果要退出JShell,只能输入/exit命令:

jshell> /exit
|  Goodbye

输入exit或按下CTRL+C键是没有任何效果的:

jshell> exit
|  Error:
|  cannot find symbol
|    symbol:   variable exit
|  exit
|  ^--^

到此这篇关于Java9新特性之命令行交互式解释器JShell的文章就介绍到这了,更多相关java解释器JShell内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文