如何在JShell中加载外部文件?

huangapple go评论70阅读模式
英文:

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(&quot;Hello world&quot;);
    }
}

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
| symbol: variable HelloWorld
from JShell. Using the /open &lt;filename&gt; command gave same result.

答案1

得分: 1

无法复现: 在问题描述中提到的("没有起作用")问题无法复现,因为它在没有问题的情况下正常工作。

这里有一个使用可工作步骤的最小可复现示例,在Java 9和Java 14上进行测试。

首先,验证Java源文件的内容:

C:\Temp&gt;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&gt;jshell HelloWorld.java
|  欢迎使用 JShell -- 版本 9.0.4
|  要获取介绍,请键入:/help intro

jshell&gt; HelloWorld.doStuff()
Hello world

正如所见,Java源文件被成功加载,static 方法被成功调用。

我们还可以使用Java 14中的JShell运行,使用/open 命令加载文件:

C:\Temp&gt;jshell
|  欢迎使用 JShell -- 版本 14
|  要获取介绍,请键入:/help intro

jshell&gt; HelloWorld.doStuff()
|  错误:
|  找不到符号
|    符号:   变量 HelloWorld
|  HelloWorld.doStuff()
|  ^--------^

jshell&gt; /open HelloWorld.java

jshell&gt; 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&gt;type HelloWorld.java
class HelloWorld
{
    public static void main(String[] argsv)
    {
    }

    public static void doStuff()
    {
        System.out.println(&quot;Hello world&quot;);
    }
}

We can run that with JShell from Java 9, loading file using command-line:

C:\Temp&gt;jshell HelloWorld.java
|  Welcome to JShell -- Version 9.0.4
|  For an introduction type: /help intro

jshell&gt; 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&gt;jshell
|  Welcome to JShell -- Version 14
|  For an introduction type: /help intro

jshell&gt; HelloWorld.doStuff()
|  Error:
|  cannot find symbol
|    symbol:   variable HelloWorld
|  HelloWorld.doStuff()
|  ^--------^

jshell&gt; /open HelloWorld.java

jshell&gt; 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.

huangapple
  • 本文由 发表于 2020年9月8日 05:37:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63784565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定