简体   繁体   English

如何运行 JShell 文件?

[英]How to run a JShell File?

I'd like to run an entire file with JShell like:我想用 JShell 运行整个文件,例如:

$ jshell my-jshell-skript.java

Where eg the content of my my-jshell-skript.java is 40 + 2;例如,我的my-jshell-skript.java的内容是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?这是现在可能的,还是我仍然必须采用旧的方式来处理 Java-Main-Class?

Edit 1: Windows-Problem编辑 1:Windows 问题

On Windows, there is still no solution for me:在 Windows 上,我仍然没有解决方案:

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. JShell 开始完全忽略我的文件。 Is it a bug?这是一个错误吗?

Edit 2: Solution for Windows-Problem编辑 2:Windows 问题的解决方案

Turns out that it is the content of my foo.原来它是我的 foo 的内容。 Seems like 1 + 1 does only work "on the fly", not read from a file:似乎1 + 1只能“即时”工作,而不是从文件中读取:

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:-您可以使用这些语句创建一个名为some.jshJshell 脚本文件,并在运行jshell的命令提示符下,将其执行为:-

jshell /path/to/some.jsh

On a MacOSX, I would do something like:在 MacOSX 上,我会执行以下操作:

在此处输入图片说明

You can pipe the string to JShell:您可以将字符串通过管道传输到 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".其中myfile包含“1 + 2”行。

JShell is not meant to run a Java class directly. JShell并不是要直接运行 Java 类。 If you want to run a java class, you still need to do it the old way - java <your-class-name> .如果你想运行一个 java 类,你仍然需要用旧的方式来做 - 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. Java Shell 工具 (JShell) 是一种用于学习 Java 编程语言和构建 Java 代码原型的交互式工具。 JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. JShell 是一个读取-计算-打印循环 (REPL),它在输入声明、语句和表达式时对其进行计算并立即显示结果。

As per this quote, JShell is meant for running or trying out individual Java statements.根据这句话,JShell 用于运行或尝试单独的 Java 语句。 In the traditional java way, you have to write a full Java program before you can run it and see the results.在传统的java方式中,您必须先编写一个完整的Java程序,然后才能运行它并查看结果。 But JShell allows you a way to try out the Java statements without needing you to build the full standalone java application.但是 JShell 允许您尝试 Java 语句,而无需构建完整的独立 Java 应用程序。

So the short answer to your question is that, no, you can't call standalone java applications like jshell my-jshell-skript.java .因此,对您的问题的简短回答是,不,您不能调用像jshell my-jshell-skript.java这样的独立 Java 应用程序。 However, you CAN call a script file which contains individual JShell commands or Java statements.但是,您可以调用包含单个 JShell 命令或 Java 语句的脚本文件。 So if you copy all the statements from your Java program and paste them to a JShell script, you can run the script like:因此,如果您从 Java 程序中复制所有语句并将它们粘贴到 JShell 脚本中,则可以像这样运行脚本:

% jshell my-jshell-skript.jsh

But this is not quite the same as running a standalone java application.但这与运行独立的 Java 应用程序并不完全相同。

Launch jshell in concise feedback mode and filter the required content-concise反馈模式启动jshell并过滤所需的内容-

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

output: 42输出: 42

More details here- How to execute java jshell command as inline from shell or windows commandLine 此处有更多详细信息- 如何从 shell 或 windows commandLine 以内联方式执行 java jshell 命令

In jshell you can save the current snippets into a file by issuing:在 jshell 中,您可以通过发出以下命令将当前片段保存到文件中:

/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 fileWindows 中,查看jsh文件的详细输出

type file.jsh | jshell -v

Problem when running jshell file.jsh运行jshell file.jshjshell 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注意:文件应该在最后一行之后包含一个空行 (/n),否则最后一行不会被执行

Pipe usage can be achieved with the "hyphen" option, absent in the initial jshell release.管道使用可以通过“连字符”选项来实现,在最初的 jshell 版本中没有。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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