本文翻译自:What does “Could not find or load main class” mean?
A common problem that new Java developers experience is that their programs fail to run with the error message: Could not find or load main class ...
新Java开发人员遇到的一个常见问题是他们的程序无法运行并显示错误消息: Could not find or load main class ...
What does this mean, what causes it, and how should you fix it? 这是什么意思,是什么原因造成的,以及应该如何解决?
#1楼
参考:https://stackoom.com/question/1Dv3Y/找不到或加载主类-是什么意思
#2楼
The java <class-name>
command syntax java <class-name>
命令语法
First of all, you need to understand the correct way to launch a program using the java
(or javaw
) command. 首先,您需要了解使用java
(或javaw
)命令启动程序的正确方法。
The normal syntax 1 is this: 常规语法1是这样的:
java [ <option> ... ] <class-name> [<argument> ...]
where <option>
is a command line option (starting with a "-" character), <class-name>
is a fully qualified Java class name, and <argument>
is an arbitrary command line argument that gets passed to your application. 其中<option>
是命令行选项(以“-”字符开头), <class-name>
是标准Java类名称,而<argument>
是传递给应用程序的任意命令行参数。
1 - There is a second syntax for "executable" JAR files which I will describe at the bottom. 1-我将在底部描述“可执行” JAR文件的第二种语法。
The fully qualified name (FQN) for the class is conventionally written as you would in Java source code; 按照惯例,该类的全限定名(FQN)就像在Java源代码中一样; eg 例如
packagename.packagename2.packagename3.ClassName
However some versions of the java
command allow you to use slashes instead of periods; 但是,某些版本的java
命令允许您使用斜杠代替句点。 eg 例如
packagename/packagename2/packagename3/ClassName
which (confusingly) looks like a file pathname, but isn't one. 哪个(令人困惑)看起来像文件路径名,但不是一个。 Note that the term fully qualified name is standard Java terminology ... not something I just made up to confuse you :-) 请注意,术语“ 完全限定名”是标准的Java术语……不是我刚刚编造出来的,以使您感到困惑:-)
Here is an example of what a java
command should look like: 这是一个java
命令应如下所示的示例:
java -Xmx100m com.acme.example.ListUsers fred joe bert
The above is going to cause the java
command to do the following: 以上将导致java
命令执行以下操作:
- Search for the compiled version of the
com.acme.example.ListUsers
class. 搜索com.acme.example.ListUsers
类的编译版本。 - Load the class. 加载课程。
- Check that the class has a
main
method with signature , return type and modifiers given bypublic static void main(String[])
. 检查该类是否具有main
方法,该方法具有public static void main(String[])
给出的签名 , 返回类型和修饰符 。 (Note, the method argument's name is NOT part of the signature.) (请注意,方法参数的名称不是签名的一部分。) - Call that method passing it the command line arguments ("fred", "joe", "bert") as a
String[]
. 调用该方法,以String[]
将命令行参数(“ fred”,“ joe”,“ bert”)传递给它。
Reasons why Java cannot find the class Java找不到类的原因
When you get the message "Could not find or load main class ...", that means that the first step has failed. 当您收到消息“找不到或加载主类...”时,表明第一步已失败。 The j