36

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.

czerny
  • 15,090
  • 14
  • 68
  • 96
  • 4
    I've been using a .jsh file extension to indicate a file to be run with jshell, since .java implies it can be compiled by javac. – Jason Apr 13 '17 at 10:43

3 Answers3

73

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

Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
Jason
  • 7,356
  • 4
  • 41
  • 48
8

Another possibility is to have an exit file:

exit.jsh

/exit

And then run:

./jshell --startup myscript.jsh --startup exit.jsh
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
0

Another option:

cat /tmp/shell.java | jshell -

Should work the same in bash and windows powershell

Mike Twc
  • 2,230
  • 2
  • 14
  • 19