1. Overview
1.概述
Occasionally when we run a Java program, we might see “Could not find or load main class.” It’s easy to guess the reason: The JVM failed to find the main class and gave this error. But why couldn’t it?
偶尔,当我们运行一个Java程序时,我们可能会看到 “无法找到或加载主类”。这很容易猜到原因。JVM没能找到主类,于是出现了这个错误。但为什么找不到呢?
In this tutorial, we’ll discuss the probable reasons for failure to find the main class. We’ll also see how to fix them.
在本教程中,我们将讨论找不到主类的可能原因。我们还将看到如何解决这些问题。
2. Sample Program
2.程序样本
We’ll start with a HelloWorld program:
我们将从一个HelloWorld程序开始。
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world..!!!");
}
}
Now let’s compile it:
现在我们来编译它。
$ javac HelloWorld.java
Here, the compiler will generate a .class file for our program. This .class file will be generated in the same directory. The .class file will have the same name as the class name given in the Java program. This .class file is executable.
这里,编译器将为我们的程序生成一个.class文件。这个.class文件将在同一目录下生成。这个.class文件的名称将与Java程序中给出的类名称相同。这个.class文件是可执行的。
In the following sections, we’ll run this .class file and try to understand the probable reasons for error “Could not find or load main class.”
在下面的章节中,我们将运行这个.class文件,并尝试了解错误 “无法找到或加载主类 “的可能原因。
3. Wrong Class Name
3.错误的班级名称
To run a .class file generated by Java compiler, we can use this command:
要运行一个由Java编译器生成的.class文件,我们可以使用这个命令。
java <.class filename>
Now let’s run our program:
现在我们来运行我们的程序。
$ java helloworld
Error: Could not find or load main class helloworld
And it failed with the error “Could not find or load main class helloworld.”
结果失败了,错误是 “无法找到或加载主类helloworld”。
As discussed earlier, the compiler will generate the .class file with the exact same name given to the Java class in the program. So in our case, the main class will have the name HelloWorld, not helloworld.
如前所述,编译器将生成.class文件,其名称与程序中的Java类完全相同。因此在我们的案例中,主类的名称是HelloWorld,而不是helloworld。
Let’s give it one more try with correct casing:
让我们用正确的外壳再试一试。
$ java HelloWorld
Hello world..!!!
This time it ran successfully.
这一次,它成功地运行了。
3.1. File Extension
3.1 文件扩展
To compile a Java program, we must provide the file name with its extension (.java):
要编译一个Java程序,我们必须提供带有扩展名(.java)的文件名。
$ javac HelloWorld.java
But to run a .class file, we need to provide the class name, not the file name. So there is no need to provide the .class extension:
但是要运行一个.class文件,我们需要提供类名,而不是文件名。所以不需要提供.class扩展名。
$ java HelloWorld.class
Error: Could not find or load main class HelloWorld.class
Again, let’s run our program using the correct class name:
再次,让我们使用正确的类名来运行我们的程序。
$ java HelloWorld
Hello world..!!!
4. Java Package Names
4.java包的名称
In Java, we keep similar classes together in what we call a package.
在Java中,我们把类似的类放在一起,称为package。
Let’s move HelloWorld class into the com.baeldung package:
让我们把HelloWorld类移到com.baeldung包。
package com.baeldung;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world..!!!");
}
}
Now let’s compile and run the updated HelloWorld program like before:
现在让我们像以前一样编译并运行更新的HelloWorld程序。
$ java HelloWorld
Error: Could not find or load main class HelloWorld
But again, we get the error “Could not find or load main class HelloWorld.”
但是,我们再次得到错误 “无法找到或加载主类HelloWorld”。
Let’s try to understand what we missed here.
让我们试着了解我们在这里错过了什么。
To run a Java class that is in a package, we must provide its fully qualified name. So in our case, HelloWorld‘s fully qualified name is com.baeldung.HelloWorld.
要运行一个包中的Java类,我们必须提供它的完全合格的名字。所以在我们的例子中,HelloWorld的完全合格的名字是com.baeldung.HelloWorld。
Now, when we created com.baeldung package, we actually created this folder structure:
现在,当我们创建com.baeldung包时,我们实际上创建了这个文件夹结构。
com/baeldung/HelloWorld.java
First, let’s try to run our program from the com/baeldung directory:
首先,让我们尝试从com/baeldung目录下运行我们的程序。
$ java com.baeldung.HelloWorld
Error: Could not find or load main class com.baeldung.HelloWorld
Still, we are not able to run our program.
但是,我们仍然无法运行我们的程序。
Here, when we specified the fully qualified class name com.baeldung.HelloWorld, Java tried to find the HelloWorld.class file in com/baeldung, under the directory from where we were running the program.
在这里,当我们指定了完全合格的类名com.baeldung.HelloWorld,Java试图在com/baeldung中找到HelloWorld.class文件,在我们运行程序的目录下。
As we were already inside com/baeldung, Java failed to find and run the HelloWorld program.
由于我们已经在com/baeldung内,Java未能找到并运行HelloWorld程序。
Now let’s move back to the parent folder and run it:
现在让我们移回父文件夹并运行它。
$ java com.baeldung.HelloWorld
Hello world..!!!
And we are again able to say “Hello” to the world.
而我们又能向世界说 “你好 “了。
5. Invalid Classpath
5.无效的分类路径
Before going ahead, let’s first understand what the classpath is. It’s the set of classes available to our currently running JVM.
在继续之前,让我们首先了解一下什么是classpath。它是我们当前运行的JVM可用的类的集合。
We use the classpath variable to tell the JVM where to find the .class files on the file system.
我们使用classpath变量来告诉JVM在哪里可以找到文件系统中的.class文件。
While running a program, we can provide the classpath using -classpath option:
当运行一个程序时,我们可以使用-classpath选项提供classpath。
java -classpath /my_programs/compiled_classes HelloWorld
Here, Java will look for the HelloWorld.class file in /my_programs/compiled_classes folder, a folder whose name we just made up. By default, the classpath variable is set to “.”, meaning the current directory.
在这里,Java将在/my_programs/compiled_classes文件夹中寻找HelloWorld.class文件,这个文件夹的名字是我们刚起的。默认情况下,classpath变量被设置为”.”,即当前目录。
In the above section, we changed our directory to run our program. But what if we want to run it from some other folder? That’s when the classpath variable helps us.
在上节中,我们改变了目录来运行我们的程序。但如果我们想从其他文件夹中运行它呢?这时classpath变量就会帮助我们。
To run our program from the directory com/baeldung, we can simply state that our classpath is two directories up — one for each package part:
为了从com/baeldung目录中运行我们的程序,我们可以简单地说明我们的classpath是向上的两个目录–每个包部分都有一个。
$ java -claspath ../../ com.baeldung.HelloWorld
Hello world..!!!
Here, “..” represents the parent directory. In our case “../../” represents the top of our package hierarchy.
这里,”… “代表父目录。在我们的例子中,”…/…/”代表我们包层次结构的顶端。
6. Conclusion
6.结语
In this article, we learned the probable reasons for the error “Could not find or load main class.”
在这篇文章中,我们了解了 “无法找到或加载主类 “错误的可能原因。
Then, of course, we also learned how to solve this error.
然后,当然,我们也学会了如何解决这个错误。