简体   繁体   English

Java找不到方法main

[英]Java can't find method main

Im having trouble with a simple hello world program lol! 我有一个简单的你好世界计划的麻烦哈哈! Im hoping someone can shed some light on this. 我希望有人可以对此有所了解。

So the error im receiving is the following: 所以收到的错误如下:

$ javac Hello.java
$ java Hello
Exception in thread "main" java.lang.NoSuchMethodError: main

So by the error, I can see that it's obviously missing main, but it's there: 因此,通过错误,我可以看到它显然缺少主要,但它在那里:

class Hello
{
  public static void main(String argv)
  {
    System.out.println("hello world");
  }
}

I'm on Mac OS/X if it's any help. 如果有任何帮助,我在Mac OS / X上。

Problem is that your method does not take String array as a argument. 问题是您的方法不将String数组作为参数。 Use following signature instead: 请改用以下签名:

public static void main(String[] argv)

or 要么

public static void main(String argv[])

Other valid option is: 其他有效选项是:

public static void main(String ... argv)

In Java Language Specification this is told as follows: 在Java语言规范中,这被告知如下:

The method main must be declared public, static, and void. 方法main必须声明为public,static和void。 It must specify a formal parameter (§8.4.1) whose declared type is array of String. 它必须指定一个形式参数(第8.4.1节),其声明的类型是String数组。

You forgot about [] in String[] argv or ... in String... argv . 你忘了[]String[] argv...String... argv This array is used to store arguments used in command creating JVM for your class like 此数组用于存储在为类创建JVM的命令中使用的参数

java Hello argument0 argument1 argument2` 

and so on. 等等。

Main method has signature that accepts String[] and you wrote String which is wrong, 主方法有签名接受String[]并且你写了错误的String

Make it 做了

public static void main(String[] argv)

or varargs 或者varargs

public static void main(String... argv)

你忘了放数组语法,你甚至可以按照JAVA 1.5使用varargs

public static void main(String... argv)

problem is with main signature, which should be 问题是main签名,应该是

public static void main(String[] argv)

or could be 或者可能是

public static void main(String ... argv) // known as varargs

instead of public static void main(String argv) which is in your case 而不是在您的情况下public static void main(String argv)

have a look at this 看看这个

for varargs look 为varargs

public static void main(String[] args)
public static void main(String... args)
public static void main(String args[])

Java programs start executing at the main method, which has the above method prototype Java程序开始在main方法执行,该方法具有上述方法原型

Your main method signature is wrong String instead of String [] 你的主要方法签名是错误的String而不是String []

use 采用

public static void main(String[] argv)

or 要么

public static void main(String... argv)

read here 在这里阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM