Echo-Beispiel von Jshell ausführen [Duplikat]
Sep 08 2020
Ich habe ein solches Beispiel für Befehlszeilenargumente gelesen:
public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
Es wird ordnungsgemäß über die Befehlszeile ausgeführt. Wie kann ich es über Jshell ausführen?
jshell> Echo.main testing
| created variable testing, however, it cannot be referenced until class main is declared
Es wird ein Fehler gemeldet, auf den nicht verwiesen werden konnte.
Antworten
2 aioobe Sep 08 2020 at 15:51
Sie rufen es wie jede andere statische Methode auf:
Echo.main(new String[] { "hello", "world" });
Vollständige Sitzung:
$ 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
Beachten Sie, dass Sie Ihre mainMethode wie folgt deklarieren können :
public static void main(String... args) { ... }
Dies ist binär kompatibel mit der String[] argsSyntax, aber Sie können sie wie folgt aufrufen:
Echo.main("hello", "world");