Jshell'den yankı örneği çalıştırın [duplicate]

Sep 08 2020

Böyle bir komut satırı argümanı örneği okudum:

public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}

Komut satırından düzgün çalışıyor, Jshell'den nasıl çalıştırabilirim?

jshell> Echo.main testing
|  created variable testing, however, it cannot be referenced until class main is declared

Başvurulamayan hatayı rapor eder.

Yanıtlar

2 aioobe Sep 08 2020 at 15:51

Başka bir statik yöntem olarak çağırırsınız:

Echo.main(new String[] { "hello", "world" });

Tam oturum:

$ 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

mainYönteminizi aşağıdaki gibi açıklayabileceğinizi unutmayın :

public static void main(String... args) { ... }

Bu, String[] argssözdizimiyle ikili uyumludur , ancak onu aşağıdaki gibi çalıştırmanıza izin verir:

Echo.main("hello", "world");