7

It seems, that JShell object created inside another JShell does not have access to parent's JShell scope. For instance:

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)]

Is it somehow possible to make parent scope visible to the child one?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241

1 Answers1

7

According to this the one big caveat about JShell is: it runs in its own JVM.

The javadoc for create() says:

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

And when you follow to the javadoc for build(), you find:

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.

In other words: most likely, you are creating another JVM instance where that other shell runs. So at least for now: no chances of having a child jshell know about its parent.

( as in: I seriously hope that this REPL feature of Java will allow at some future point to attach a JShell to an already running JVM )

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Yep, that's true, it starts a new JVM. From other hand, I thought it could be some kind of special methods in JShell class which allow to bind somehow it with parent JShell. I tend to accept your answer ,but let's wait a little for other ideas – Andremoniy Sep 14 '17 at 10:07
  • 1
    @Andremoniy Being used to [jython](http://www.jython.org/) I was really really disappointed upon finding out that jshell can **not** hook into an existing JVM. This restriction takes away half of the "usefulness" of this feature. But maybe Java 10 or so and they fix that part. – GhostCat Sep 14 '17 at 10:10