如何运行JShell文件? [英] How to run a JShell File?

查看:145
本文介绍了如何运行JShell文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JShell运行整个文件,例如:

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

$ jshell my-jshell-skript.java

例如我的my-jshell-skript.java的内容是40 + 2;.

或可执行文件,例如:

#!/usr/bin/jshell
40 + 2

现在有可能吗?还是我仍然需要沿用Java-Main-Class的旧方法?

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

在Windows上,仍然没有适合我的解决方案:

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开始完全忽略我的文件.是虫子吗?

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

原来,这是我的foo的内容.似乎1 + 1只能即时"运行,而不能从文件中读取:

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>

推荐答案

JShell并不意味着直接运行Java类.如果要运行Java类,则仍然需要使用旧方法-java <your-class-name>.

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>.

Java Shell工具(JShell)是用于学习以下内容的交互式工具: Java编程语言和Java代码原型. JShell是一个 读取-评估-打印循环(REPL),用于评估声明, 输入的语句和表达式,并立即显示 结果.

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.

根据此引言,JShell用于运行或试用单个Java语句.用传统的Java方式,必须先编写完整的Java程序,然后才能运行它并查看结果.但是,JShell允许您尝试Java语句的方法,而无需构建完整的独立Java应用程序.

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.

因此,对您的问题的简短回答是,不,您不能调用独立的Java应用程序,例如jshell my-jshell-skript.java.但是,您可以调用包含单个JShell命令或Java语句的脚本文件.因此,如果您从Java程序中复制所有语句并将其粘贴到JShell脚本中,则可以运行以下脚本:

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

但这与运行独立的Java应用程序并不完全相同.

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

这篇关于如何运行JShell文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆