在 JShell 中,如何评估整个 java 代码?
In JShell, How to evaluate whole java code?
我正在使用 JShell API 到 运行 Java 代码。但是,当我 运行 整个代码时出现错误。
例如:
import jdk.jshell.JShell;
var code= """
void hello(){
System.out.println("Hello world");
}
hello();
""";
JShell shell = JShell.create();
List<SnippetEvent> snippets = shell.eval(code);
评估后,我在输出片段中看到了错误。
cannot find symbol
symbol: method hello()
location: class
这里有什么问题?
eval
不是为处理这样的代码而设计的。您显示的代码实际上是 2 个“完整片段”,在您进入 eval
之前需要将其分解。来自 docs:
The input should be exactly one complete snippet of source code, that is, one expression, statement, variable declaration, method declaration, class declaration, or import. To break arbitrary input into individual complete snippets, use SourceCodeAnalysis.analyzeCompletion(String)
.
“真正的”JShell 命令行程序可能使用 SourceCodeAnalysis.analyzeCompletion(String)
方法将您的输入分解为两个完整的片段,并将每个片段传递给 eval
.
这里是SourceCodeAnalysis.analyzeCompletion(String)
的使用方法:
var code = "...";
JShell jshell = JShell.create();
SourceCodeAnalysis.CompletionInfo completionInfo = jshell.sourceCodeAnalysis().analyzeCompletion(code);
// this is the shortest complete code
System.out.println(completionInfo.source());
// this is what remains after removing the shortest complete code
// you'd probably want to analyze the completion of this recursively
System.out.println(completionInfo.remaining());
hello();
编译失败的原因与
相同
class Foo {
void hello() {
}
hello();
}
无法编译。
如果您查看诊断:
JShell shell = JShell.create();
List<SnippetEvent> events = shell.eval(code);
shell.diagnostics(events.get(0).snippet()).forEach(x -> System.out.println(x.getMessage(Locale.ENGLISH)));
你得到:
Invalid method declaration; return type required
如果您将方法调用置于 class 级别,这就是您收到的确切错误消息。
无论如何,要让您的代码正常工作,只需先 eval
方法声明,然后 eval
hello();
调用。
我正在使用 JShell API 到 运行 Java 代码。但是,当我 运行 整个代码时出现错误。
例如:
import jdk.jshell.JShell;
var code= """
void hello(){
System.out.println("Hello world");
}
hello();
""";
JShell shell = JShell.create();
List<SnippetEvent> snippets = shell.eval(code);
评估后,我在输出片段中看到了错误。
cannot find symbol
symbol: method hello()
location: class
这里有什么问题?
eval
不是为处理这样的代码而设计的。您显示的代码实际上是 2 个“完整片段”,在您进入 eval
之前需要将其分解。来自 docs:
The input should be exactly one complete snippet of source code, that is, one expression, statement, variable declaration, method declaration, class declaration, or import. To break arbitrary input into individual complete snippets, use
SourceCodeAnalysis.analyzeCompletion(String)
.
“真正的”JShell 命令行程序可能使用 SourceCodeAnalysis.analyzeCompletion(String)
方法将您的输入分解为两个完整的片段,并将每个片段传递给 eval
.
这里是SourceCodeAnalysis.analyzeCompletion(String)
的使用方法:
var code = "...";
JShell jshell = JShell.create();
SourceCodeAnalysis.CompletionInfo completionInfo = jshell.sourceCodeAnalysis().analyzeCompletion(code);
// this is the shortest complete code
System.out.println(completionInfo.source());
// this is what remains after removing the shortest complete code
// you'd probably want to analyze the completion of this recursively
System.out.println(completionInfo.remaining());
hello();
编译失败的原因与
class Foo {
void hello() {
}
hello();
}
无法编译。
如果您查看诊断:
JShell shell = JShell.create();
List<SnippetEvent> events = shell.eval(code);
shell.diagnostics(events.get(0).snippet()).forEach(x -> System.out.println(x.getMessage(Locale.ENGLISH)));
你得到:
Invalid method declaration; return type required
如果您将方法调用置于 class 级别,这就是您收到的确切错误消息。
无论如何,要让您的代码正常工作,只需先 eval
方法声明,然后 eval
hello();
调用。