英文:
How do you load an external file in JShell?
问题
JShell是用于Java的交互式REPL命令行。
如果我有一个带有一些方法的Java类,我想在一个.java文件中进行交互式操作,我该如何加载该文件?
假设我有文件HelloWorld.java
:
class HelloWorld
{
public static void main(String[] argsv)
{
}
public static void doStuff()
{
System.out.println("Hello world");
}
}
我想加载JShell并能够从命令行调用doStuff()
方法。我该如何做?
使用文件名启动JShell
,例如$ JShell HelloWorld.java
并不起作用。也不是classfile。我仍然从JShell中得到错误cannot find symbol | symbol: variable HelloWorld
。使用/open <filename>
命令得到相同的结果。
英文:
JShell is the interactive REPL command line for Java.
If I have a Java class with some methods that I would like to play around with interactively in a .java file, how do I load that file in?
Let's say I have the file HelloWorld.java
:
class HelloWorld
{
public static void main(String[] argsv)
{
}
public static void doStuff()
{
System.out.println("Hello world");
}
}
and I'd like to load up JShell and be able to call the doStuff()
method from the command line. How do I do that?
Starting JShell
with the file name as $ JShell HelloWorld.java
didn't work. Nor classfile. I still get the error cannot find symbol
from JShell. Using the
| symbol: variable HelloWorld/open <filename>
command gave same result.
答案1
得分: 1
无法复现: 在问题描述中提到的("没有起作用")问题无法复现,因为它在没有问题的情况下正常工作。
这里有一个使用可工作步骤的最小可复现示例,在Java 9和Java 14上进行测试。
首先,验证Java源文件的内容:
C:\Temp>type HelloWorld.java
class HelloWorld
{
public static void main(String[] argsv)
{
}
public static void doStuff()
{
System.out.println("Hello world");
}
}
我们可以使用Java 9中的JShell来运行,使用命令行加载文件:
C:\Temp>jshell HelloWorld.java
| 欢迎使用 JShell -- 版本 9.0.4
| 要获取介绍,请键入:/help intro
jshell> HelloWorld.doStuff()
Hello world
正如所见,Java源文件被成功加载,static
方法被成功调用。
我们还可以使用Java 14中的JShell运行,使用/open
命令加载文件:
C:\Temp>jshell
| 欢迎使用 JShell -- 版本 14
| 要获取介绍,请键入:/help intro
jshell> HelloWorld.doStuff()
| 错误:
| 找不到符号
| 符号: 变量 HelloWorld
| HelloWorld.doStuff()
| ^--------^
jshell> /open HelloWorld.java
jshell> HelloWorld.doStuff()
Hello world
我们首先尝试在执行/open
命令之前运行,以证明HelloWorld
不存在,即证明是/open
命令声明了该类。
英文:
Unable to reproduce: The problem described in the question ("didn't work") is not reproducible, since it's working without issue.
Here is a Minimal, Reproducible Example with the working steps, testing on Java 9 and Java 14.
First, to verify the content of the Java source file:
C:\Temp>type HelloWorld.java
class HelloWorld
{
public static void main(String[] argsv)
{
}
public static void doStuff()
{
System.out.println("Hello world");
}
}
We can run that with JShell from Java 9, loading file using command-line:
C:\Temp>jshell HelloWorld.java
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro
jshell> HelloWorld.doStuff()
Hello world
As can be seen, the Java source file is loaded just fine, and the static
method is called without issue.
We can also run that with JShell from Java 14, loading file using /open
command:
C:\Temp>jshell
| Welcome to JShell -- Version 14
| For an introduction type: /help intro
jshell> HelloWorld.doStuff()
| Error:
| cannot find symbol
| symbol: variable HelloWorld
| HelloWorld.doStuff()
| ^--------^
jshell> /open HelloWorld.java
jshell> HelloWorld.doStuff()
Hello world
We first tried to run before the /open
command, to prove that HelloWorld
did not exist, i.e. proving that it is the /open
command that declares the class.
答案2
得分: 1
我遇到了类似的问题,但在我的情况下,无论出于什么原因,除非我从类声明中移除public
,否则该类在通过/open
命令加载时都不会生效。我不知道这是否与我的jshell
版本有关(11.0.2,在MacOSX Catalina上运行),还是与jshell
本身有关的特定问题。
英文:
I was having a similar issue to this but in my case for whatever reason the class wouldn't load via the /open
command unless I removed public
from the class declaration. I don't know if this was do to a bug in my version of jshell
(11.0.2 running on MacOSX Catalina), or it's something specific about jshell itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论