java - 在 JShell 中访问 "parent scope"

标签 java java-9 jshell

看来,在另一个 JShell 中创建的 JShell 对象无法访问父级的 JShell 范围。例如:

jshell> int x = 1;
x ==> 1

jshell> x
x ==> 1

jshell> jdk.jshell.JShell js = jdk.jshell.JShell.create();
js ==> jdk.jshell.JShell@1a052a00

jshell> js.eval("x");
$4 ==> [SnippetEvent(snippet=Snippet:ErroneousKey#1-x,previousStatus=NONEXISTENT,status=REJECTED,isSignatureChange=false,causeSnippetnull)]

jshell> js.eval("int x = 2;");
$5 ==> [SnippetEvent(snippet=Snippet:VariableKey(x)#2-int x = 2;,previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=2)]

jshell> js.eval("x");
$6 ==> [SnippetEvent(snippet=Snippet:ExpressionKey(x)#3-x,previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=2)]

是否有可能使父范围对子范围可见?

最佳答案

根据 this关于 JShell 的一个重要警告是:它在其自己的 JVM 中运行。

create() 的 javadoc说:

Equivalent to JShell.builder().build().

当您关注 build() 的 javadoc 时,你会发现:

Build a JShell state engine. This is the entry-point to all JShell functionality. This creates a remote process for execution. It is thus important to close the returned instance.

换句话说:您很可能正在创建运行其他 shell 的另一个 JVM 实例。所以至少现在:没有机会让 child jshell 知道它的父级。

(如:我真心希望 Java 的这个 REPL 特性在未来某个时候允许将 JShell 附加到已经运行的 JVM )

关于java - 在 JShell 中访问 "parent scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46214641/

相关文章:

java - 错误 : java. lang.InternalError : Failed remote launch: com. sun.jdi.CommandLineLaunch

java - 如果项目与条件匹配,是否有办法跳出 java 流?

java - 对象发布保证线程安全

java - 在java中调用方法并在for循环中递增数组

java - JDK9有动态模块发现吗?

java - 模块化项目中测试代码的实用打包

java - 运行应用程序时出错 : No JDK specified

java-9 - 如何将参数传递给jshell脚本?

java - Tapestry @Persist(PersistenceConstants.FLASH) 与 Tomcat 相关,但与 Jetty 无关

android-studio - Android Studio 中的 JShell 控制台