Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a java script with jshell?

Tags:

Given that Java 9 is upon us and we can finally have a java REPL with jshell I was hoping there was a way to add a shebang to a script and have jshell interpret it.

I tried creating test.jsh:

#!/usr/bin/env jshell -s System.out.println("Hello World") /exit 

However that gives:

⚡ ./test.jsh |  Error: |  illegal character: '#' |  #!/usr/bin/env jshell -s |  ^ |  Error: |  illegal start of expression |  #!/usr/bin/env jshell -s |    ^ Hello World 

It turns out there is an enhancement request for this in OpenJDK https://bugs.openjdk.java.net/browse/JDK-8167440.

Is there any other way to do this?

like image 280
steinybot Avatar asked Jul 05 '17 03:07

steinybot


People also ask

How do I run a Java file in JShell?

To load a file into the JShell tool, we can use the "/open" command. The "/open" command has loaded the "Test. java" file into a session. The "/vars" command can be used to load the variables into a session and "/methods" command can be used to load the methods into a session.

How do I run a JShell script?

To run a script with JShell, we simply need to put the path to the script at the first parameter of the JShell command while to exit JShell after executing, we just need to put the /exit command at the end of the script.

How do I use JShell in terminal?

To start Jshell, first we must have installed Java 9 then open terminal in Linux or command prompt in windows and type jshell. It will start jshell session and displays a welcome message to the console. To display a simple “Hello Java” message, write print command without creating class and hit enter.

What is a JShell script?

A JShell script is a sequence of snippets and JShell commands in a file, one snippet or command per line. Scripts can be a local file, or one of the following predefined scripts: Script Name. Script Contents. DEFAULT.


2 Answers

Use

//usr/bin/env jshell --show-version --execution local "$0" "$@"; exit $?

as the first line of test.jsh. The test.jsh script could look like:

//usr/bin/env jshell --show-version "$0" "$@"; exit $? System.out.println("Hello World") /exit 

The command line option --show-version is optional, of course, but gives immediate feedback that the tool is running.

The extra command line option --execution local prevents jshell to spawn another VM. This speeds up launch time and if an exception is thrown by your script code, the local VM will exit.

Consult the output of jshell --help and jshell --help-extra for more options.

Update

Also take a look at https://github.com/jbangdev/jbang Having fun with Java scripting, which offers a neat wrapper around running .java files from the command line.

like image 171
Sormuras Avatar answered Sep 19 '22 08:09

Sormuras


It turns out that with a bit of trickery there is a way, although I haven't fully managed to suppress the interpreted commands but pretty close to what I want.

Change test.jsh to:

#!/usr/bin/env sh tail -n +4 "$0" | jshell -s "$@" exit $? System.out.println("Hello World") /exit 

Which gives us:

⚡ ./test.jsh -> System.out.println("Hello World") Hello World -> /exit 
like image 37
steinybot Avatar answered Sep 21 '22 08:09

steinybot