Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - How to run a JShell File?

I'd like to run an entire file with JShell like:

$ jshell my-jshell-skript.java

Where e.g. the content of my my-jshell-skript.java is 40 + 2;.

Or alternatively an executable like:

#!/usr/bin/jshell
40 + 2

Is this possible now or do I still have to take the old way over a Java-Main-Class?

Edit 1: Windows-Problem

On Windows, there is still no solution for me:

C:JDKsjdk9.0.0.0_x64in>type foo.jsh
1 + 1

C:JDKsjdk9.0.0.0_x64in>jshell.exe foo.jsh
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> /exit
|  Goodbye

C:JDKsjdk9.0.0.0_x64in>

JShell starts ignoring my file completely. Is it a bug?

Edit 2: Solution for Windows-Problem

Turns out that it is the content of my foo. Seems like 1 + 1 does only work "on the fly", not read from a file:

C:JDKsjdk9.0.0.0_x64in>type foo.jsh
System.out.println("foo");

C:JDKsjdk9.0.0.0_x64in>jshell.exe foo.jsh
foo
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> /exit
|  Goodbye

C:JDKsjdk9.0.0.0_x64in>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a Jshell script file named some.jsh with those statements and on the command prompt from where you run jshell, execute it as:-

jshell /path/to/some.jsh

On a MacOSX, I would do something like:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...