Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shutdown jshell at the end of the script?

How to instruct jshell to terminate at the end of the script similarly to interpreters of other languages like for example python3 or node?

Following command

./jshell -q /tmp/shell.java 

with script /tmp/shell.java

System.out.println("Hello world"); 

prints

Hello world jshell>  

and waits for further commands. I'd like it to stop immediately at the end of the file.

I'm looking for something more elegant than System.exit(0); at the end of the script.

like image 550
czerny Avatar asked Apr 12 '17 19:04

czerny


People also ask

How do you close a JShell?

To exit JShell, type /exit .

How do I open a JShell in CMD?

To start JShell, enter the jshell command on the command line. JDK 9 must be installed on your system. If your path doesn't include java-home/jdk-9/bin , start the tool from within that directory.

Can we create methods with some return type using JShell?

Using MethodsA method is declared in JShell just as it is in a Java application, with a few differences, which are also discussed in this section. As an example, declare a method triple(int) that takes an int argument and returns an int value. Run the code snippet in JShell and a method gets created.


1 Answers

Inside the script, use the jshell command /exit. This will exit jshell at the end of your script.

Check this reference https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm#JSHEL-GUID-465BA4F5-E77D-456F-BCB7-D826AC1E18AE

like image 194
Jason Avatar answered Oct 13 '22 00:10

Jason