Exécuter un exemple d'écho à partir de Jshell [duplicate]
Sep 08 2020
J'ai lu un tel exemple d'arguments de ligne de commande:
public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
Il fonctionne correctement à partir de la ligne de commande, comment pourrais-je l'exécuter à partir de Jshell?
jshell> Echo.main testing
| created variable testing, however, it cannot be referenced until class main is declared
Il signale une erreur qui n'a pas pu être référencée.
Réponses
2 aioobe Sep 08 2020 at 15:51
Vous l'appelez comme n'importe quelle autre méthode statique:
Echo.main(new String[] { "hello", "world" });
Session complète:
$ 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
Notez que vous pouvez déclarer votre mainméthode comme suit:
public static void main(String... args) { ... }
Ceci est compatible binaire avec la String[] argssyntaxe, mais vous permettrait de l'invoquer comme suit:
Echo.main("hello", "world");