我读过这样一个命令行参数示例:
public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}它从命令行正确运行,我如何从Jshell运行它?
jshell> Echo.main testing
| created variable testing, however, it cannot be referenced until class main is declared它报告未能被引用的错误。
发布于 2020-09-08 08:51:13
您可以像任何其他静态方法一样调用它:
Echo.main(new String[] { "hello", "world" });整届会议:
$ jshell
| Welcome to JShell -- Version 11.0.8
| For an introduction type: /help intro
jshell> public class Echo {
...> public static void main (String[] args) {
...> for (String s: args) {
...> System.out.println(s);
...> }
...> }
...> }
| created class Echo
jshell> Echo.main(new String[] { "hello", "world" });
hello
world注意,您可以如下所示声明您的main方法:
public static void main(String... args) { ... }这是与String[] args语法兼容的二进制文件,但允许您按以下方式调用它:
Echo.main("hello", "world");https://stackoverflow.com/questions/63789556
复制相似问题