Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you use the <code>java</code> command to run a Java application from the command line, e.g.,</p> <pre><code> java some.AppName arg1 arg2 ... </code></pre> <p>the command loads the class that you nominated, and then looks for the entry point method called <code>main</code>. More specifically, it is looking for a method that is declared as follows:</p> <pre><code>package some; public class AppName { ... public static void main(String[] args) { // body of main method follows ... } } </code></pre> <p>The specific requirements for the entry point method are:</p> <ol> <li>The method must be in the nominated class.</li> <li>The name of the method must be "main" with <em>exactly</em> that capitalization<sup>1</sup>.</li> <li>The method must be <code>public</code>.</li> <li>The method must be <code>static</code> <sup>2</sup>.</li> <li>The method's return type must be <code>void</code>.</li> <li>The method must have exactly one argument and argument's type must be <code>String[]</code> <sup>3</sup>. </li> </ol> <p>The argument <em>may</em> be declared using varargs syntax; e.g. <code>String... args</code>. See <a href="https://stackoverflow.com/a/36803396/139985">https://stackoverflow.com/a/36803396/139985</a>. The <code>String[]</code> argument is used to pass the arguments from the command line, and is required even if your application takes no command line arguments.</p> <p>If any one of the above requirements is not satisfied, the <code>java</code> command will fail with the message:</p> <pre><code>java.lang.NoSuchMethodError: main Exception in thread “main” </code></pre> <p>If you encounter this error, check that you have a <code>main</code> method and that it satisfies all 6 of the requirements listed above.</p> <hr> <p><sup>1 - One really obscure variation of this is when one or more of the characters in "main" is NOT a LATIN-1 character ... but a Unicode character that <em>looks like</em> the corresponding LATIN-1 character when displayed.</sup></p> <p><sup>2 - See <a href="https://stackoverflow.com/q/146576/781723">Why is the Java main method static?</a> for explanation of why the method is required to be static.</sup></p> <p><sup>3 - <code>String</code> must correspond to <code>java.lang.String</code> and not a custom class named <code>String</code> hiding it.</sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload