How to run the entire Java file added as a fragment in JShell?

I tried installing the 172 Early Access JDK version 9 to play with JShell. When I tried to open a simple java file and execute it after adding it as a fragment, it simply showed the modified class test and increased the fragment number. Could you help me understand where I was wrong?

| Welcome to JShell -- Version 9-ea | For an introduction type: /help intro jshell> /open G:\kavitha\Test.java jshell> /list 1 : public class Test{ public static void main(String[] args){ int i = 1; int j = 2; System.out.println("Sum of 1 and 2 is : " + (i+j)); } } jshell> /1 public class Test{ public static void main(String[] args){ int i = 1; int j = 2; System.out.println("Sum of 1 and 2 is : " + (i+j)); } } | modified class Test jshell> /list 2 : public class Test{ public static void main(String[] args){ int i = 1; int j = 2; System.out.println("Sum of 1 and 2 is : " + (i+j)); } } 
+5
source share
1 answer

/open only downloads the file, after which you should tell jshell what you want to execute.

Example:

 jshell> /open Test.java jshell> /list 1 : class Test { public static void main(String[] args) { System.out.println("Hello Kavitha"); } int rollDice() { return 1 + (int)(6 * Math.random()); } } jshell> Test.main(new String[0]) Hello Kavitha jshell> new Test().rollDice() $3 ==> 3 

Here I executed the main method, but I can also use the loaded class as I want, create a new instance, call the method, etc.

The shortcut /<id> restarts the fragment with this id. In your case, fragment 1 only loads the class and does nothing, so by doing /1 you reloaded the same class without having to execute it again.

After running my example above, /2 will repeat the launch of the main method, and /3 will repeat the roll of the bone:

 jshell> /3 new Test().rollDice() $4 ==> 1 jshell> /3 new Test().rollDice() $5 ==> 6 jshell> /3 new Test().rollDice() $6 ==> 2 

Addendum: /open and compilation, class loading, class initialization

(Trying to find out why /open did not execute the main method of your class and show that it makes sense)

When you /open file, JShell will add the contents of the file as if you entered it in a shell.

Then it compiles the fragments, but will not initialize the classes, if any.

(I'm not sure whether to load classes, which is a step before initialization, it's hard to say, see this post , which was an attempt to examine the internal components of JShell, it shows how class names in JShell are translated for the user, and unsuccessful attempts to see the list loaded classes, but this is less important than compilation and initialization)

If I open SnippetWithError.txt , which contains the following:

 System.out.println("Hey") class Foo { int n=33; } bar 

(yes, it does not have to be a java file, it really is a bunch of text that you include in the shell for evaluation)

 jshell> /open SnippetWithError.txt Hey | Error: | cannot find symbol | symbol: variable bar | bar | ^-^ 

Look at it "Hey" and it included the Foo class:

 jshell> new Foo().n $2 ==> 33 

So, JShell compiles when you /open , it executes the instructions, but if some of the statements are declarations of classes or methods, it does not execute them, it does not even initialize the classes.

See below how import is treated as separate statements in history, then the class declaration is in its own statement (# 3):

 jshell> /open HasStaticInitBlock.java jshell> /list 1 : import java.time.Duration; 2 : import java.time.Instant; 3 : class HasStaticInitBlock { static Instant t0 = Instant.now(); static { System.out.println("Class initialized at " + t0); } static Duration timeSinceInit() { return Duration.between(t0, Instant.now()); } } jshell> HasStaticInitBlock.timeSinceInit() Class initialized at 2017-06-07T06:49:06.746185Z $4 ==> PT0.040414S jshell> HasStaticInitBlock.timeSinceInit() $5 ==> PT2.343019S 

Class initialization was performed only when necessary.

+8
source

Source: https://habr.com/ru/post/1268606/


All Articles