在 jshell 启动时加载脚本的不同方法

Different ways to load script on jshell startup

根据文档,

There’s also the option to load a script on startup, including some special predefined options. These are specified using the —startup flag, passing in either a filename or one of:

DEFAULT – load the default behavior. This acts the same as if this is not specified at all

JAVASE – Imports all Java SE packages by default

PRINTING – defines the functions print, println and printf for use

但是,当我使用 DEFAULT、JAVASE 或 PRINTING 选项启动 jshell 时,我可以在所有情况下导入类似的 类。

例如JAVASE涉及的主要包java.lang、java.security等,在其他选项中也有。请找到以下快照:

kv:micro-tab karan.verma$ jshell --startup JAVASE
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> import javax.security.
auth   cert   sasl   

jshell> /exit
|  Goodbye
kv:micro-tab karan.verma$ jshell --startup DEFAULT
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> import javax.security
security   

jshell> import javax.security.
auth   cert   sasl   

jshell> /exit
|  Goodbye
kv:micro-tab karan.verma$ jshell --startup PRINTING
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> import javax.security.
   ...> 
auth   cert   sasl   

我是否以正确的方式使用这些选项? 或者这些还有别的意思?

您程序中的断言方法不正确。区分这些预定义脚本的正确方法是使用

/list -start

查看所有启动片段的评估结果。

例如:-