简体   繁体   中英

How to run a JShell File?

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

$ jshell my-jshell-skript.java

Where eg 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:\JDKs\jdk9.0.0.0_x64\bin>type foo.jsh
1 + 1

C:\JDKs\jdk9.0.0.0_x64\bin>jshell.exe foo.jsh
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> /exit
|  Goodbye

C:\JDKs\jdk9.0.0.0_x64\bin>

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:\JDKs\jdk9.0.0.0_x64\bin>type foo.jsh
System.out.println("foo");

C:\JDKs\jdk9.0.0.0_x64\bin>jshell.exe foo.jsh
foo
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> /exit
|  Goodbye

C:\JDKs\jdk9.0.0.0_x64\bin>

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:

在此处输入图片说明

You can pipe the string to JShell:

echo 1 + 2 | jshell

Example:

:/# echo 1 + 2 | jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> 1 + 2
$1 ==> 3

:/#

Or, from a file:

cat myfile | jshell

Where myfile contains the line "1 + 2".

JShell is not meant to run a Java class directly. If you want to run a java class, you still need to do it the old way - java <your-class-name> .

From the docs ,

The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.

As per this quote, JShell is meant for running or trying out individual Java statements. In the traditional java way, you have to write a full Java program before you can run it and see the results. But JShell allows you a way to try out the Java statements without needing you to build the full standalone java application.

So the short answer to your question is that, no, you can't call standalone java applications like jshell my-jshell-skript.java . However, you CAN call a script file which contains individual JShell commands or Java statements. So if you copy all the statements from your Java program and paste them to a JShell script, you can run the script like:

% jshell my-jshell-skript.jsh

But this is not quite the same as running a standalone java application.

Launch jshell in concise feedback mode and filter the required content-

$echo '40 + 2' | jshell --feedback concise | sed -n '2p' |sed -En 's/[^>]*>(.+)/\1/gp'

output: 42

More details here- How to execute java jshell command as inline from shell or windows commandLine

In jshell you can save the current snippets into a file by issuing:

/save Filename

Likewise, you can load the file into the current context/session by issuing:

/open Filename

Here is one such example:

|  Welcome to JShell -- Version 9.0.7.1
|  For an introduction type: /help intro

jshell> String[] names={"nameone","nametwo"}
names ==> String[2] { "nameone", "nametwo" }

jshell> Arrays.toString(names);
$2 ==> "[nameone, nametwo]"

jshell> /save myExample

jshell> %                                                                                                                                             sudipbhandari at sysadm-Latitude-5480 in ~                                                                                                      18:22
> jshell
|  Welcome to JShell -- Version 9.0.7.1
|  For an introduction type: /help intro

jshell> names
|  Error:
|  cannot find symbol
|    symbol:   variable names
|  names
|  ^---^

jshell> /open myExample

jshell> names
names ==> String[2] { "nameone", "nametwo" }

In Windows , to see the verbose output for a jsh file

type file.jsh | jshell -v

Problem when running jshell file.jsh

D:\>type file.jsh
3 + 5

D:\>jshell file.jsh
|  Welcome to JShell -- Version 13.0.2
|  For an introduction type: /help intro

jshell>

Workaround:

D:\>type file.jsh
3 + 5

D:\>type file.jsh | jshell -v
|  Welcome to JShell -- Version 13.0.2
|  For an introduction type: /help intro

jshell> $1 ==> 8
|  created scratch variable $1 : int

jshell>

Note: file should contain a blank line (/n) after the last line, else the last line is not getting executed

Pipe usage can be achieved with the "hyphen" option, absent in the initial jshell release.

echo 'System.out.print(1 + 2)' | jshell -

https://docs.oracle.com/en/java/javase/11/tools/jshell.html
https://bugs.openjdk.java.net/browse/JDK-8187439

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM