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?
Imagine when you want to test an API, you can open jshell, type the code and get results immediately. No need to write a class, then a main method, then compile and run the program. In this tutorial, we’re going to help you understand and get the most out of jshell, for your daily Java learning. 1. Start jshell
The Java Shell comes with JDK 9 so you need to have JDK 9 installed on your computer. If the PATH environment variable is configured properly, you can type jshell anywhere in the command line prompt: These little chunks of Java code are called 'snippets'.
Thus using jshell is more efficient than edit/compile/execute and System.out.println approach. Imagine when you want to test an API, you can open jshell, type the code and get results immediately. No need to write a class, then a main method, then compile and run the program.
The jsh command invokes jShell - the jBASE shell. It can be invoked as your login shell by using the normal system administration software supplied with the platform. Either via.bat files (Windows) or.profiles (Unix).
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With