“找不到或装入主类”是什么意思?

时间:2023-01-18 10:23:39

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开发人员遇到的一个常见问题是,他们的程序无法运行错误消息:无法找到或加载主类…

What does this mean, what causes it, and how should you fix it?

这意味着什么,导致它的原因是什么,你该如何解决它?

38 个解决方案

#1


796  

The java <class-name> command syntax

First of all, you need to understand the correct way to launch a program using the java (or javaw) command.

首先,您需要理解使用java(或javaw)命令启动程序的正确方法。

The normal syntax1 is this:

正常的syntax1是这样的:

    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.
1 - There is a second syntax for "executable" JAR files which I will describe at the bottom.

其中 <选项> 是命令行选项(以“-”字符开头), 是一个完全合格的Java类名, <参数> 是一个任意的命令行参数,被传递给应用程序。1 -我将在底部描述“可执行”JAR文件的第二个语法。

The fully qualified name (FQN) for the class is conventionally written as you would in Java source code; e.g.

这个类的完全限定名(FQN)通常是按照Java源代码编写的;如。

    packagename.packagename2.packagename3.ClassName

However some versions of the java command allow you to use slashes instead of periods; e.g.

但是,java命令的一些版本允许您使用斜杠代替句号;如。

    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命令执行以下操作:

  1. Search for the compiled version of the com.acme.example.ListUsers class.
  2. 搜索com.acme.example的编译版本。ListUsers类。
  3. Load the class.
  4. 加载类。
  5. Check that the class has a main method with signature, return type and modifiers given by public static void main(String[]). (Note, the method argument's name is NOT part of the signature.)
  6. 检查类是否有一个主方法,它具有由public static void main(String[])所提供的签名、返回类型和修饰符。(注意,方法参数的名称不是签名的一部分。)
  7. Call that method passing it the command line arguments ("fred", "joe", "bert") as a String[].
  8. 调用该方法将命令行参数(“fred”、“joe”、“bert”)传递为字符串[]。

Reasons why Java cannot find the class

When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.

当您收到“无法找到或加载主类……”这意味着第一步失败了。java命令不能找到类。实际上,消息中的“…”将是java正在寻找的完全限定类名。

So why might it be unable to find the class?

那么为什么它不能找到这个类呢?

Reason #1 - you made a mistake with the classname argument

The first likely cause is that you may have provided the wrong class name. (Or ... the right class name, but in the wrong form.) Considering the example above, here a variety of wrong ways to specify the class name:

第一个可能的原因是您可能提供了错误的类名。(或…正确的类名,但格式错误。考虑到上面的例子,这里有各种错误的方法来指定类名:

  • Example #1 - a simple class name:

    例#1 -一个简单的类名:

    java ListUser
    

    When the class is declared in a package such as com.acme.example, then you must use the full classname including the package name in the java command; e.g.

    当类在包中声明时,例如com.acme。例如,必须在java命令中使用完整的classname,包括包名;如。

    java com.acme.example.ListUser
    
  • Example #2 - a filename or pathname rather than a class name:

    例#2 -文件名或路径名,而不是类名:

    java ListUser.class
    java com/acme/example/ListUser.class
    
  • Example #3 - a class name with the casing incorrect:

    例#3 -带有外壳的类名不正确:

    java com.acme.example.listuser
    
  • Example #4 - a typo

    例#4 -输入错误。

    java com.acme.example.mistuser
    
  • Example #5 - a source filename

    示例#5 -源文件名。

    java ListUser.java
    
  • Example #6 - you forgot the class name entirely

    例#6 -您完全忘记了类名。

    java lots of arguments
    

Reason #2 - the application's classpath is incorrectly specified

The second likely cause is that the class name is correct, but that the java command cannot find the class. To understand this, you need to understand the concept of the "classpath". This is explained well by the Oracle documentation:

第二个可能的原因是类名是正确的,但是java命令不能找到该类。要理解这一点,您需要理解“类路径”的概念。这在Oracle文档中得到了很好的解释:

So ... if you have specified the class name correctly, the next thing to check is that you have specified the classpath correctly:

所以…如果您已经正确指定了类名,接下来要检查的是您是否正确地指定了类路径:

  1. Read the three documents linked above. (Yes ... READ them. It is important that a Java programmer understands at least the basics of how the Java classpath mechanisms works.)
  2. 阅读上面链接的三个文档。(是的……读它们。Java程序员至少了解Java类路径机制的工作原理是很重要的。
  3. Look at command line and / or the CLASSPATH environment variable that is in effect when you run the java command. Check that the directory names and JAR file names are correct.
  4. 在运行java命令时,查看命令行和/或CLASSPATH环境变量。检查目录名称和JAR文件名是否正确。
  5. If there are relative pathnames in the classpath, check that they resolve correctly ... from the current directory that is in effect when you run the java command.
  6. 如果类路径中有相对路径名,请检查它们是否正确解析……在运行java命令时,从当前目录开始。
  7. Check that the class (mentioned in the error message) can be located on the effective classpath.
  8. 检查类(在错误消息中提到)可以位于有效的类路径上。
  9. Note that the classpath syntax is different for Windows versus Linux and Mac OS.
  10. 请注意,对于Windows和Linux和Mac OS来说,类路径语法是不同的。

Reason #2a - the wrong directory is on the classpath

When you put a directory on the classpath, it notionally corresponds to the root of the qualified name space. Classes are located in the directory structure beneath that root, by mapping the fully qualified name to a pathname. So for example, if "/usr/local/acme/classes" is on the class path, then when the JVM looks for a class called com.acme.example.Foon, it will look for a ".class" file with this pathname:

当您在类路径上放置一个目录时,它在理论上对应于限定名称空间的根。类位于根目录下的目录结构中,通过将完全限定名映射到路径名。例如,如果“/usr/local/acme/classes”位于类路径上,那么当JVM查找一个名为com.acme的类时。Foon,它会找a。类“文件与此路径名:

  /usr/local/acme/classes/com/acme/example/Foon.class

If you had put "/usr/local/acme/classes/com/acme/example" on the classpath, then the JVM wouldn't be able to find the class.

如果您在类路径上放置了“/usr/local/acme/classes/com/acme/example”,那么JVM将无法找到该类。

Reason #2b - the subdirectory path doesn't match the FQN

If your classes FQN is com.acme.example.Foon, then the JVM is going to look for "Foon.class" in the directory "com/acme/example":

如果您的类FQN是com.acme.example。Foon,然后JVM会寻找“Foon”。“目录下的目录”:

  • If your directory structure doesn't match the package naming as per the pattern above, the JVM won't find your class.

    如果您的目录结构与上面的模式不匹配包命名,那么JVM将无法找到您的类。

  • If you attempt rename a class by moving it, that will fail as well ... but the exception stacktrace will be different.

    如果您尝试通过移动它来重命名一个类,那么它也会失败。但异常堆栈跟踪将会有所不同。

To give a concrete example, supposing that:

举一个具体的例子,假设:

  • you want to run com.acme.example.Foon class,
  • 你想运行com.acme示例。Foon类,
  • the full file path is /usr/local/acme/classes/com/acme/example/Foon.class,
  • 完整的文件路径是/usr/local/acme/classes/com/acme/example/Foon.class,
  • your current working directory is /usr/local/acme/classes/com/acme/example/,
  • 您当前的工作目录是/usr/local/acme/classes/com/acme/example/,

then:

然后:

# wrong, FQN is needed
java Foon

# wrong, there is no `com/acme/example` folder in the current working directory
java com.acme.example.Foon

# wrong, similar to above
java -classpath . com.acme.example.Foon

# fine; relative classpath set
java -classpath ../../.. com.acme.example.Foon

# fine; absolute classpath set
java -classpath /usr/local/acme/classes com.acme.example.Foon

Notes:

注:

  • The -classpath option can be shortened to -cp in most Java releases. Check the respective manual entries for java, javac and so on.
  • 在大多数Java版本中,-classpath选项可以缩短为-cp。检查java、javac等的相关手工条目。
  • Think carefully when choosing between absolute and relative pathnames in classpaths. Remember that a relative pathname may "break" if the current directory changes.
  • 在类路径的绝对和相对路径名之间选择时要仔细考虑。请记住,如果当前目录发生更改,相对路径名可能会“中断”。

Reason #2c - dependencies missing from the classpath

The classpath needs to include all of the other (non-system) classes that your application depends on. (The system classes are located automatically, and you rarely need to concern yourself with this.) For the main class to load correctly, the JVM needs to find:

类路径需要包含应用程序所依赖的所有其他(非系统)类。(系统类是自动定位的,您很少需要关注这一点。)对于要正确加载的主类,JVM需要找到:

  • the class itself.
  • 类本身。
  • all classes and interfaces in the superclass hierarchy (e.g. see Java class is present in classpath but startup fails with Error: Could not find or load main class)
  • 超类层次结构中的所有类和接口(例如,在类路径中出现了Java类,但是启动失败了:无法找到或装入主类)
  • all classes and interfaces that are referred to by means of variable or variable declarations, or method call or field access expressions.
  • 通过变量或变量声明或方法调用或字段访问表达式引用的所有类和接口。

(Note: the JLS and JVM specifications allow some scope for a JVM to load classes "lazily", and this can affect when a classloader exception is thrown.)

(注意:JLS和JVM规范允许JVM的某些范围“延迟”加载类,这可能会影响类加载器异常的发生。)

Reason #3 - the class has been declared in the wrong package

It occasionally happens that someone puts a source code file into the the wrong folder in their source code tree, or they leave out the package declaration. If you do this in an IDE, the IDE's compiler will tell you about this immediately. Similarly if you use a decent Java build tool, the tool will run javac in a way that will detect the problem. However, if you build your Java code by hand, you can do it in such a way that the compiler doesn't notice the problem, and the resulting ".class" file is not in the place that you expect it to be.

有时会有人将源代码文件放入源代码树中错误的文件夹中,或者省略了包声明。如果您在IDE中这样做,IDE的编译器会立即告诉您这一点。类似地,如果您使用一个像样的Java构建工具,该工具将以一种能够检测到问题的方式运行javac。但是,如果您手工构建Java代码,您可以这样做,这样编译器就不会注意到问题和结果。类“文件不在您期望的位置”。

The java -jar <jar file> syntax

The alternative syntax used for "executable" JAR files is as follows:

用于“可执行”JAR文件的替代语法如下:

  java [ <option> ... ] -jar <jar-file-name> [<argument> ...]

e.g.

如。

  java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred

In this case the name of the entry-point class (i.e. com.acme.example.ListUser) and the classpath are specified in the MANIFEST of the JAR file.

在这种情况下,入口点类的名称(即com.acme.example.ListUser)和类路径在JAR文件的清单中指定。

IDEs

A typical Java IDE has support for running Java applications in the IDE JVM itself or in a child JVM. These are generally immune from this particular exception, because the IDE uses its own mechanisms to construct the runtime classpath, identify the main class and create the java command line.

典型的Java IDE支持在IDE JVM中或在子JVM中运行Java应用程序。这些通常都不受这个特殊的异常的影响,因为IDE使用它自己的机制来构造运行时类路径,识别主类并创建java命令行。

However it is still possible for this exception to occur, if you do things behind the back of the IDE. For example, if you have previously set up an Application Launcher for your Java app in Eclipse, and you then moved the JAR file containing the "main" class to a different place in the file system without telling Eclipse, Eclipse would unwittingly launch the JVM with an incorrect classpath.

但是,如果您在IDE背后做一些事情,仍然有可能出现这个异常。例如,如果您之前在Eclipse中为Java应用程序设置了一个应用程序启动程序,然后您将包含“main”类的JAR文件移动到文件系统中的另一个位置,而不告诉Eclipse, Eclipse会在不经意间启动带有错误类路径的JVM。

In short, if you get this problem in an IDE, check for things like stale IDE state, broken project references or broken launcher configurations.

简而言之,如果您在IDE中遇到这个问题,请检查陈旧的IDE状态、损坏的项目引用或损坏的启动程序配置。

It is also possible for an IDE to simply get confused. IDE's are hugely complicated pieces of software comprising many interacting parts. Many of these parts adopt various caching strategies in order to make the IDE as a whole responsive. These can sometimes go wrong, and one possible symptom is problems when launching applications. If you suspect this could be happening, it is worth restarting your IDE.

IDE也有可能会被混淆。IDE是非常复杂的软件,包含许多相互作用的部分。其中许多部分采用各种缓存策略,以使IDE具有整体响应能力。这些问题有时会出错,在启动应用程序时可能出现问题。如果您怀疑这可能发生,那么重新启动IDE是值得的。

Other References

#2


176  

If your source code name is HelloWorld.java, your compiled code will be HelloWorld.class.

如果您的源代码是HelloWorld。java,编译后的代码将是HelloWorld.class。

You will get that error if you call it using:

如果你调用它,你会得到这个错误:

java HelloWorld.class

Instead, use this:

相反,用这个:

java HelloWorld

#3


88  

If your classes are in packages then you have to cd to the main directory and run using the full name of the class (packageName.MainClassName).

如果您的类在包中,那么您必须将cd映射到主目录,并使用类的全名(packageName.MainClassName)运行。

Example:

例子:

My classes are in here:

我的课程在这里:

D:\project\com\cse\

The full name of my main class is:

我的主要班级的全称是:

com.cse.Main

So I cd back to the main directory:

所以我回到主目录:

D:\project

Then issue the java command:

然后发出java命令:

java com.cse.Main

#4


38  

If your main method is in the class under a package, you should run it over the hierarchical directory.

如果您的主要方法是在包下的类中,您应该在分层目录下运行它。

Assume there is a source code file (Main.java):

假设有一个源代码文件(Main.java):

package com.test;

public class Main {

    public static void main(String[] args) {
        System.out.println("salam 2nya\n");
    }
}

For running this code, you should place Main.Class in the package like directory ./com/test/Main.Java. And in the root directory use java com.test.Main.

对于运行此代码,您应该放置Main。类在包中,如目录。/com/test/Main.Java。在根目录中使用java .test. main。

#5


31  

When the same code works on one PC, but it shows the error in another, the best solution I have ever found is compiling like the following:

当相同的代码在一个PC上运行时,但是它显示了另一个PC上的错误,我所找到的最好的解决方案是编译如下:

javac HelloWorld.java
java -cp . HelloWorld

#6


26  

What helped me was specifying the classpath on the command line, for example:

帮助我的是在命令行上指定类路径,例如:

  1. Create a new folder, C:\temp

    创建一个新的文件夹,C:\temp。

  2. Create file Temp.java in C:\temp, with the following class in it:

    在C:\temp中创建文件Temp.java:\temp,其中包含以下类:

    public class Temp {
        public static void main(String args[]) {
            System.out.println(args[0]);
        }
    }
    
  3. Open a command line in folder C:\temp, and write the following command to compile the Temp class:

    打开文件夹C:\temp中的命令行,并编写以下命令来编译temp类:

    javac Temp.java
    
  4. Run the compiled Java class, adding the -classpath option to let JRE know where to find the class:

    运行编译后的Java类,添加-classpath选项,让JRE知道在哪里找到类:

    java -classpath C:\temp Temp Hello!
    

#7


18  

According to the error message ("Could not find or load main class"), there are two categories of problems:

根据错误消息(“无法找到或加载主类”),存在以下两类问题:

  1. Main class could not be found
  2. 无法找到主类。
  3. Main class could not be loaded (this case is not fully discussed in the accepted answer)
  4. 主类无法加载(此情况未在已接受的答案中充分讨论)

Main class could not be found when there is typo or wrong syntax in the fully qualified class name or it does not exist in the provided classpath.

当在完全限定的类名中出现了错误语法或者在提供的类路径中不存在时,无法找到主类。

Main class could not be loaded when the class cannot be initiated, typically the main class extends another class and that class does not exist in the provided classpath.

当类不能被启动时,主类不能被加载,通常,主类扩展另一个类,而该类在提供的类路径中不存在。

For example:

例如:

public class YourMain extends org.apache.camel.spring.Main

If camel-spring is not included, this error will be reported.

如果不包括camel-spring,将会报告此错误。

#8


11  

Sometimes what might be causing the issue has nothing to do with the main class, and I had to find this out the hard way. It was a referenced library that I moved, and it gave me the:

有时引起这个问题的原因与主要的课程无关,而我必须通过困难的方式来解决这个问题。它是我移动的参考图书馆,它给了我:

Could not find or load main class xxx Linux

无法找到或装入主类xxx Linux ?

I just deleted that reference, added it again, and it worked fine again.

我刚刚删除了那个引用,又添加了它,它又恢复正常了。

#9


11  

I had such an error in this case:

在这种情况下,我犯了这样一个错误:

java -cp lib.jar com.mypackage.Main

It works with ; for Windows and : for Unix:

它的工作原理;适用于Windows和:Unix:

java -cp lib.jar; com.mypackage.Main

#10


8  

Try -Xdiag.

-Xdiag试试。

Steve C's answer covers the possible cases nicely, but sometimes to determine whether the class could not be found or loaded might not be that easy. Use java -Xdiag (since JDK 7). This prints out a nice stacktrace which provides a hint to what the message Could not find or load main class message means.

Steve C的回答很好地涵盖了可能的情况,但有时要确定这个类是否被发现或加载可能不是那么容易。使用java -Xdiag(因为JDK 7)。这将打印出一个漂亮的堆栈跟踪,它提供了一个提示,提示消息无法找到或加载主类消息的含义。

For instance, it can point you to other classes used by the main class that could not be found and prevented the main class to be loaded.

例如,它可以指向无法找到的主类使用的其他类,并阻止主类加载。

#11


6  

In this instance you have:

在这个例子中,你有:

Could not find or load main class ?classpath

无法找到或装入主类?类路径?

It's because you are using "-classpath", but the dash is not the same dash used by java on the command prompt. I had this issue copying and pasting from Notepad to cmd.

这是因为您使用的是“-classpath”,但是dash并不是java在命令提示符上使用的相同的破折号。我从记事本复制粘贴到cmd。

#12


6  

Use this command:

使用这个命令:

java -cp . [PACKAGE.]CLASSNAME

Example: If your classname is Hello.class created from Hello.java then use the below command:

例子:如果你的类名是Hello。类创建的你好。然后使用下面的命令:

java -cp . Hello

If your file Hello.java is inside package com.demo then use the below command

你好如果你的文件。java在包内部。演示然后使用下面的命令。

java -cp . com.demo.Hello

With JDK 8 many times it happens that the class file is present in the same folder, but the java command expects classpath and for this reason we add -cp . to take the current folder as reference for classpath.

使用JDK 8很多次,类文件出现在同一个文件夹中,但是java命令期望类路径,因此我们添加-cp。将当前文件夹作为类路径的引用。

#13


5  

In my case, error appeared because I had supplied the source file name instead of the class name.

在我的例子中,出现错误是因为我提供了源文件名而不是类名。

We need to supply the class name containing the main method to the interpreter.

我们需要向解释器提供包含主方法的类名。

#14


4  

“找不到或装入主类”是什么意思?

Class file location: C:\test\com\company

类文件位置:C:\ \ com \公司测试

File Name: Main.class

文件名称:Main.class

Fully qualified class name: com.company.Main

全限定类名:com.company.Main。

Command line command:

命令行命令:

java  -classpath "C:\test" com.company.Main

Note here that class path does NOT include \com\company

注意,类路径不包括\com\company。

#15


4  

This might help you if your case is specifically like mine: as a beginner I also ran into this problem when I tried to run a Java program.

如果你的情况和我的一样,这可能会对你有所帮助:作为一个初学者,我在尝试运行Java程序时也遇到了这个问题。

I compiled it like this:

我这样编译:

javac HelloWorld.java

And I tried to run also with the same extension:

我也试着用同样的扩展来运行:

java Helloworld.java

When I removed the .java and rewrote the command like java HelloWorld, the program ran perfectly. :)

当我删除了.java并重写了像java HelloWorld这样的命令时,程序运行得很好。:)

#16


3  

First set the path using this command;

首先使用此命令设置路径;

set path="paste the set path address"

Then you need to load the program. Type "cd (folder name)" in the stored drive and compile it. For Example, if my program stored on the D drive, type "D:" press enter and type " cd (folder name)".

然后你需要加载程序。在存储驱动器中键入“cd(文件夹名)”并编译它。例如,如果我的程序存储在D驱动器上,键入“D:”按enter并键入“cd(文件夹名)”。

#17


3  

This is a specific case, but since I came to this page looking for a solution and didn't find it, I'll add it here.

这是一个具体的例子,但由于我来到这个页面寻找解决方案,并没有找到它,我将在这里添加它。

Windows (tested with 7) doesn't accept special characters (like á) in class and package names. Linux does, though.

Windows(用7测试)不接受类和包名中的特殊字符(比如a)。虽然Linux,。

I found this out when I built a .jar in NetBeans and tried to run it in command line. It ran in NetBeans but not in command line.

当我在NetBeans中构建一个.jar并试图在命令行运行它时,我发现了这个问题。它在NetBeans中运行,但不在命令行中。

#18


3  

What fixed the problem in my case was:

在我的案例中,解决的问题是:

Right click on the project/class you want to run, then Run As->Run Configurations. Then you should either fix your existing configuration or add new in the following way:

右键单击要运行的项目/类,然后运行As->运行配置。然后,您要么修改现有配置,要么以以下方式添加新配置:

open the Classpath tab, click on the Advanced... button then add bin folder of your project.

打开Classpath选项卡,单击Advanced…按钮然后添加项目的bin文件夹。

#19


3  

If you use Maven to build the JAR file, please make sure to specify the main class in the pom.xml file:

如果您使用Maven构建JAR文件,请确保在pom中指定主类。xml文件:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>class name us.com.test.abc.MyMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

#20


3  

I spent a decent amount of time trying to solve this problem. I thought that I was somehow setting my classpath incorrectly but the problem was that I typed:

我花了相当多的时间来解决这个问题。我认为我的路径设置不正确但问题是我输入了:

java -cp C:/java/MyClasses C:/java/MyClasses/utilities/myapp/Cool  

instead of:

而不是:

java -cp C:/java/MyClasses utilities/myapp/Cool   

I thought the meaning of fully qualified meant to include the full path name instead of the full package name.

我认为完全限定的含义应该包括完整的路径名,而不是完整的包名。

#21


2  

On Windows put .; at the CLASSPATH value in the beginning.

在Windows上把。在开始时的CLASSPATH值。

The . (dot) means "look in the current directory". This is a permanent solution.

的。(dot)表示“查看当前目录”。这是一个永久的解决方案。

Also you can set it "one time" with set CLASSPATH=%CLASSPATH%;.. This will last as long as your cmd window is open.

您还可以将它设置为“一次”,设置类路径=%CLASSPATH%;..这将持续只要您的cmd窗口打开。

#22


2  

All answers here are directed towards Windows users it seems. For Mac, the classpath separator is :, not ;. As an error setting the classpath using ; is not thrown then this can be a difficult to discover if coming from Windows to Mac.

所有的答案都指向Windows用户。对于Mac,类路径分隔符是:,而不是;。作为设置类路径的错误;如果从Windows到Mac,就很难发现这一点。

Here is corresponding Mac command:

以下是对应的Mac命令:

java -classpath ".:./lib/*" com.test.MyClass

Where in this example the package is com.test and a lib folder is also to be included on classpath.

在这个示例中,包是com。测试和lib文件夹也包含在类路径中。

#23


2  

When running the java with the -cp option as advertised in Windows PowerShell you may get an error that looks something like:

当在Windows PowerShell中使用-cp选项运行java时,您可能会得到如下错误:

The term `ClassName` is not recognized as the name of a cmdlet, function, script ...

In order to for PowerShell to accept the command, the arguments of the -cp option must be contained in quotes as in:

为了让PowerShell接受命令,-cp选项的参数必须包含在引号中:

java -cp 'someDependency.jar;.' ClassName

Forming the command this way should allow Java process the classpath arguments correctly.

通过这种方式形成命令,可以正确地允许Java处理类路径参数。

#24


2  

Sometimes, in some online compilers that you might have tried you will get this error if you don't write public class [Classname] but just class [Classname].

有时候,在一些在线编译器中,如果您不编写公共类(类名),而只编写类[Classname],您可能会得到这个错误。

#25


2  

In the context of IDE development (Eclipse, NetBeans or whatever) you have to configure your project properties to have a main class, so that your IDE knows where the main class is located to be executed when you hit "Play".

在IDE开发(Eclipse、NetBeans或其他)的环境中,您必须配置项目属性以拥有一个主类,以便您的IDE知道当您点击“Play”时,将在何处执行主类。

  1. Right click your project, then Properties
  2. 右键单击项目,然后是属性。
  3. Go to the Run category and select your Main Class
  4. 转到Run类别并选择您的主类。
  5. Hit the Run button.
  6. 点击运行按钮。

“找不到或装入主类”是什么意思?

#26


1  

In Java, when you sometimes run the JVM from the command line using the java executable and are trying to start a program from a class file with public static void main (PSVM), you might run into the below error even though the classpath parameter to the JVM is accurate and the class file is present on the classpath:

在Java中,当你有时从命令行运行JVM使用Java可执行文件和试图启动一个项目从一个类文件公共静态孔隙主要(PSVM),你可能会遇到以下错误即使JVM类路径参数是准确和类路径上的类文件存在:

Error: main class not found or loaded

This happens if the class file with PSVM could not be loaded. One possible reason for that is that the class may be implementing an interface or extending another class that is not on the classpath. Normally if a class is not on the classpath, the error thrown indicates as such. But, if the class in use is extended or implemented, java is unable to load the class itself.

如果使用PSVM的类文件无法加载,就会发生这种情况。其中一个可能的原因是,该类可能正在实现一个接口或扩展另一个不在类路径上的类。正常情况下,如果类不在类路径上,则抛出的错误指示为这样。但是,如果使用的类被扩展或实现,java就无法装入类本身。

Reference: https://www.computingnotes.net/java/error-main-class-not-found-or-loaded/

参考:https://www.computingnotes.net/java/error-main-class-not-found-or-loaded/

#27


1  

I got this error after doing mvn eclipse:eclipse This messed up my .classpath file a little bit.

在做了mvn eclipse之后,我得到了这个错误:eclipse把我的.classpath文件弄得一团糟。

Had to change the lines in .classpath from

需要更改.classpath中的行吗?

<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>

to

<classpathentry kind="src" path="src/main/java" output="target/classes" />
<classpathentry kind="src" path="src/main/resources" excluding="**"  output="target/classes" />

#28


1  

You really need to do this from the src folder. There you type the following command line:

您确实需要从src文件夹中执行此操作。在这里输入以下命令行:

[name of the package].[Class Name] [arguments]

Let's say your class is called CommandLine.class, and the code looks like this:

假设你的类叫做命令行。类,代码如下所示:

package com.tutorialspoint.java;

    /**
     * Created by mda21185 on 15-6-2016.
     */

    public class CommandLine {
        public static void main(String args[]){
            for(int i=0; i<args.length; i++){
                System.out.println("args[" + i + "]: " + args[i]);
            }
        }
    }

Then you should cd to the src folder and the command you need to run would look like this:

然后,您应该将cd映射到src文件夹,您需要运行的命令如下所示:

java com.tutorialspoint.java.CommandLine this is a command line 200 -100

And the output on the command line would be:

命令行上的输出是:

args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

#29


1  

In my case, I got the error because I had mixed UPPER- and lower-case package names on a Windows 7 system. Changing the package names to all lower case resolved the issue. Note also that in this scenario, I got no error compiling the .java file into a .class file; it just wouldn't run from the same (sub-sub-sub-) directory.

在我的例子中,我得到了错误,因为我在Windows 7系统上混合了大小写包名。将包名更改为所有小写字母都解决了这个问题。还要注意,在这个场景中,我没有错误地将.java文件编译成.class文件;它不会从相同的(sub-sub-)目录运行。

#30


0  

I was unable to solve this problem with the solutions stated here (although the answer stated has, no doubt, cleared my concepts). I faced this problem two times and each time I have tried different solutions (in the Eclipse IDE).

我无法用此处所述的解决方案来解决这个问题(尽管答案声明无疑地澄清了我的概念)。我两次遇到这个问题,每次尝试不同的解决方案(在Eclipse IDE中)。

  • Firstly, I have come across with multiple main methods in different classes of my project. So, I had deleted the main method from subsequent classes.
  • 首先,我在我的项目的不同类别中遇到了多种主要方法。因此,我已经删除了后续类的主要方法。
  • Secondly, I tried following solution:
    1. Right click on my main project directory.
    2. 右键单击我的主项目目录。
    3. Head to source then clean up and stick with the default settings and on Finish. After some background tasks you will be directed to your main project directory.
    4. 头到源,然后清理和坚持默认设置和完成。在一些后台任务之后,您将被定向到您的主项目目录。
    5. After that I close my project, reopen it, and boom, I finally solved my problem.
    6. 在那之后,我关闭了我的项目,重新打开它,然后,我终于解决了我的问题。
  • 其次,我尝试了以下解决方案:右键单击我的主项目目录。头到源,然后清理和坚持默认设置和完成。在一些后台任务之后,您将被定向到您的主项目目录。在那之后,我关闭了我的项目,重新打开它,然后,我终于解决了我的问题。

#1


796  

The java <class-name> command syntax

First of all, you need to understand the correct way to launch a program using the java (or javaw) command.

首先,您需要理解使用java(或javaw)命令启动程序的正确方法。

The normal syntax1 is this:

正常的syntax1是这样的:

    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.
1 - There is a second syntax for "executable" JAR files which I will describe at the bottom.

其中 <选项> 是命令行选项(以“-”字符开头), 是一个完全合格的Java类名, <参数> 是一个任意的命令行参数,被传递给应用程序。1 -我将在底部描述“可执行”JAR文件的第二个语法。

The fully qualified name (FQN) for the class is conventionally written as you would in Java source code; e.g.

这个类的完全限定名(FQN)通常是按照Java源代码编写的;如。

    packagename.packagename2.packagename3.ClassName

However some versions of the java command allow you to use slashes instead of periods; e.g.

但是,java命令的一些版本允许您使用斜杠代替句号;如。

    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命令执行以下操作:

  1. Search for the compiled version of the com.acme.example.ListUsers class.
  2. 搜索com.acme.example的编译版本。ListUsers类。
  3. Load the class.
  4. 加载类。
  5. Check that the class has a main method with signature, return type and modifiers given by public static void main(String[]). (Note, the method argument's name is NOT part of the signature.)
  6. 检查类是否有一个主方法,它具有由public static void main(String[])所提供的签名、返回类型和修饰符。(注意,方法参数的名称不是签名的一部分。)
  7. Call that method passing it the command line arguments ("fred", "joe", "bert") as a String[].
  8. 调用该方法将命令行参数(“fred”、“joe”、“bert”)传递为字符串[]。

Reasons why Java cannot find the class

When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.

当您收到“无法找到或加载主类……”这意味着第一步失败了。java命令不能找到类。实际上,消息中的“…”将是java正在寻找的完全限定类名。

So why might it be unable to find the class?

那么为什么它不能找到这个类呢?

Reason #1 - you made a mistake with the classname argument

The first likely cause is that you may have provided the wrong class name. (Or ... the right class name, but in the wrong form.) Considering the example above, here a variety of wrong ways to specify the class name:

第一个可能的原因是您可能提供了错误的类名。(或…正确的类名,但格式错误。考虑到上面的例子,这里有各种错误的方法来指定类名:

  • Example #1 - a simple class name:

    例#1 -一个简单的类名:

    java ListUser
    

    When the class is declared in a package such as com.acme.example, then you must use the full classname including the package name in the java command; e.g.

    当类在包中声明时,例如com.acme。例如,必须在java命令中使用完整的classname,包括包名;如。

    java com.acme.example.ListUser
    
  • Example #2 - a filename or pathname rather than a class name:

    例#2 -文件名或路径名,而不是类名:

    java ListUser.class
    java com/acme/example/ListUser.class
    
  • Example #3 - a class name with the casing incorrect:

    例#3 -带有外壳的类名不正确:

    java com.acme.example.listuser
    
  • Example #4 - a typo

    例#4 -输入错误。

    java com.acme.example.mistuser
    
  • Example #5 - a source filename

    示例#5 -源文件名。

    java ListUser.java
    
  • Example #6 - you forgot the class name entirely

    例#6 -您完全忘记了类名。

    java lots of arguments
    

Reason #2 - the application's classpath is incorrectly specified

The second likely cause is that the class name is correct, but that the java command cannot find the class. To understand this, you need to understand the concept of the "classpath". This is explained well by the Oracle documentation:

第二个可能的原因是类名是正确的,但是java命令不能找到该类。要理解这一点,您需要理解“类路径”的概念。这在Oracle文档中得到了很好的解释:

So ... if you have specified the class name correctly, the next thing to check is that you have specified the classpath correctly:

所以…如果您已经正确指定了类名,接下来要检查的是您是否正确地指定了类路径:

  1. Read the three documents linked above. (Yes ... READ them. It is important that a Java programmer understands at least the basics of how the Java classpath mechanisms works.)
  2. 阅读上面链接的三个文档。(是的……读它们。Java程序员至少了解Java类路径机制的工作原理是很重要的。
  3. Look at command line and / or the CLASSPATH environment variable that is in effect when you run the java command. Check that the directory names and JAR file names are correct.
  4. 在运行java命令时,查看命令行和/或CLASSPATH环境变量。检查目录名称和JAR文件名是否正确。
  5. If there are relative pathnames in the classpath, check that they resolve correctly ... from the current directory that is in effect when you run the java command.
  6. 如果类路径中有相对路径名,请检查它们是否正确解析……在运行java命令时,从当前目录开始。
  7. Check that the class (mentioned in the error message) can be located on the effective classpath.
  8. 检查类(在错误消息中提到)可以位于有效的类路径上。
  9. Note that the classpath syntax is different for Windows versus Linux and Mac OS.
  10. 请注意,对于Windows和Linux和Mac OS来说,类路径语法是不同的。

Reason #2a - the wrong directory is on the classpath

When you put a directory on the classpath, it notionally corresponds to the root of the qualified name space. Classes are located in the directory structure beneath that root, by mapping the fully qualified name to a pathname. So for example, if "/usr/local/acme/classes" is on the class path, then when the JVM looks for a class called com.acme.example.Foon, it will look for a ".class" file with this pathname:

当您在类路径上放置一个目录时,它在理论上对应于限定名称空间的根。类位于根目录下的目录结构中,通过将完全限定名映射到路径名。例如,如果“/usr/local/acme/classes”位于类路径上,那么当JVM查找一个名为com.acme的类时。Foon,它会找a。类“文件与此路径名:

  /usr/local/acme/classes/com/acme/example/Foon.class

If you had put "/usr/local/acme/classes/com/acme/example" on the classpath, then the JVM wouldn't be able to find the class.

如果您在类路径上放置了“/usr/local/acme/classes/com/acme/example”,那么JVM将无法找到该类。

Reason #2b - the subdirectory path doesn't match the FQN

If your classes FQN is com.acme.example.Foon, then the JVM is going to look for "Foon.class" in the directory "com/acme/example":

如果您的类FQN是com.acme.example。Foon,然后JVM会寻找“Foon”。“目录下的目录”:

  • If your directory structure doesn't match the package naming as per the pattern above, the JVM won't find your class.

    如果您的目录结构与上面的模式不匹配包命名,那么JVM将无法找到您的类。

  • If you attempt rename a class by moving it, that will fail as well ... but the exception stacktrace will be different.

    如果您尝试通过移动它来重命名一个类,那么它也会失败。但异常堆栈跟踪将会有所不同。

To give a concrete example, supposing that:

举一个具体的例子,假设:

  • you want to run com.acme.example.Foon class,
  • 你想运行com.acme示例。Foon类,
  • the full file path is /usr/local/acme/classes/com/acme/example/Foon.class,
  • 完整的文件路径是/usr/local/acme/classes/com/acme/example/Foon.class,
  • your current working directory is /usr/local/acme/classes/com/acme/example/,
  • 您当前的工作目录是/usr/local/acme/classes/com/acme/example/,

then:

然后:

# wrong, FQN is needed
java Foon

# wrong, there is no `com/acme/example` folder in the current working directory
java com.acme.example.Foon

# wrong, similar to above
java -classpath . com.acme.example.Foon

# fine; relative classpath set
java -classpath ../../.. com.acme.example.Foon

# fine; absolute classpath set
java -classpath /usr/local/acme/classes com.acme.example.Foon

Notes:

注:

  • The -classpath option can be shortened to -cp in most Java releases. Check the respective manual entries for java, javac and so on.
  • 在大多数Java版本中,-classpath选项可以缩短为-cp。检查java、javac等的相关手工条目。
  • Think carefully when choosing between absolute and relative pathnames in classpaths. Remember that a relative pathname may "break" if the current directory changes.
  • 在类路径的绝对和相对路径名之间选择时要仔细考虑。请记住,如果当前目录发生更改,相对路径名可能会“中断”。

Reason #2c - dependencies missing from the classpath

The classpath needs to include all of the other (non-system) classes that your application depends on. (The system classes are located automatically, and you rarely need to concern yourself with this.) For the main class to load correctly, the JVM needs to find:

类路径需要包含应用程序所依赖的所有其他(非系统)类。(系统类是自动定位的,您很少需要关注这一点。)对于要正确加载的主类,JVM需要找到:

  • the class itself.
  • 类本身。
  • all classes and interfaces in the superclass hierarchy (e.g. see Java class is present in classpath but startup fails with Error: Could not find or load main class)
  • 超类层次结构中的所有类和接口(例如,在类路径中出现了Java类,但是启动失败了:无法找到或装入主类)
  • all classes and interfaces that are referred to by means of variable or variable declarations, or method call or field access expressions.
  • 通过变量或变量声明或方法调用或字段访问表达式引用的所有类和接口。

(Note: the JLS and JVM specifications allow some scope for a JVM to load classes "lazily", and this can affect when a classloader exception is thrown.)

(注意:JLS和JVM规范允许JVM的某些范围“延迟”加载类,这可能会影响类加载器异常的发生。)

Reason #3 - the class has been declared in the wrong package

It occasionally happens that someone puts a source code file into the the wrong folder in their source code tree, or they leave out the package declaration. If you do this in an IDE, the IDE's compiler will tell you about this immediately. Similarly if you use a decent Java build tool, the tool will run javac in a way that will detect the problem. However, if you build your Java code by hand, you can do it in such a way that the compiler doesn't notice the problem, and the resulting ".class" file is not in the place that you expect it to be.

有时会有人将源代码文件放入源代码树中错误的文件夹中,或者省略了包声明。如果您在IDE中这样做,IDE的编译器会立即告诉您这一点。类似地,如果您使用一个像样的Java构建工具,该工具将以一种能够检测到问题的方式运行javac。但是,如果您手工构建Java代码,您可以这样做,这样编译器就不会注意到问题和结果。类“文件不在您期望的位置”。

The java -jar <jar file> syntax

The alternative syntax used for "executable" JAR files is as follows:

用于“可执行”JAR文件的替代语法如下:

  java [ <option> ... ] -jar <jar-file-name> [<argument> ...]

e.g.

如。

  java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred

In this case the name of the entry-point class (i.e. com.acme.example.ListUser) and the classpath are specified in the MANIFEST of the JAR file.

在这种情况下,入口点类的名称(即com.acme.example.ListUser)和类路径在JAR文件的清单中指定。

IDEs

A typical Java IDE has support for running Java applications in the IDE JVM itself or in a child JVM. These are generally immune from this particular exception, because the IDE uses its own mechanisms to construct the runtime classpath, identify the main class and create the java command line.

典型的Java IDE支持在IDE JVM中或在子JVM中运行Java应用程序。这些通常都不受这个特殊的异常的影响,因为IDE使用它自己的机制来构造运行时类路径,识别主类并创建java命令行。

However it is still possible for this exception to occur, if you do things behind the back of the IDE. For example, if you have previously set up an Application Launcher for your Java app in Eclipse, and you then moved the JAR file containing the "main" class to a different place in the file system without telling Eclipse, Eclipse would unwittingly launch the JVM with an incorrect classpath.

但是,如果您在IDE背后做一些事情,仍然有可能出现这个异常。例如,如果您之前在Eclipse中为Java应用程序设置了一个应用程序启动程序,然后您将包含“main”类的JAR文件移动到文件系统中的另一个位置,而不告诉Eclipse, Eclipse会在不经意间启动带有错误类路径的JVM。

In short, if you get this problem in an IDE, check for things like stale IDE state, broken project references or broken launcher configurations.

简而言之,如果您在IDE中遇到这个问题,请检查陈旧的IDE状态、损坏的项目引用或损坏的启动程序配置。

It is also possible for an IDE to simply get confused. IDE's are hugely complicated pieces of software comprising many interacting parts. Many of these parts adopt various caching strategies in order to make the IDE as a whole responsive. These can sometimes go wrong, and one possible symptom is problems when launching applications. If you suspect this could be happening, it is worth restarting your IDE.

IDE也有可能会被混淆。IDE是非常复杂的软件,包含许多相互作用的部分。其中许多部分采用各种缓存策略,以使IDE具有整体响应能力。这些问题有时会出错,在启动应用程序时可能出现问题。如果您怀疑这可能发生,那么重新启动IDE是值得的。

Other References

#2


176  

If your source code name is HelloWorld.java, your compiled code will be HelloWorld.class.

如果您的源代码是HelloWorld。java,编译后的代码将是HelloWorld.class。

You will get that error if you call it using:

如果你调用它,你会得到这个错误:

java HelloWorld.class

Instead, use this:

相反,用这个:

java HelloWorld

#3


88  

If your classes are in packages then you have to cd to the main directory and run using the full name of the class (packageName.MainClassName).

如果您的类在包中,那么您必须将cd映射到主目录,并使用类的全名(packageName.MainClassName)运行。

Example:

例子:

My classes are in here:

我的课程在这里:

D:\project\com\cse\

The full name of my main class is:

我的主要班级的全称是:

com.cse.Main

So I cd back to the main directory:

所以我回到主目录:

D:\project

Then issue the java command:

然后发出java命令:

java com.cse.Main

#4


38  

If your main method is in the class under a package, you should run it over the hierarchical directory.

如果您的主要方法是在包下的类中,您应该在分层目录下运行它。

Assume there is a source code file (Main.java):

假设有一个源代码文件(Main.java):

package com.test;

public class Main {

    public static void main(String[] args) {
        System.out.println("salam 2nya\n");
    }
}

For running this code, you should place Main.Class in the package like directory ./com/test/Main.Java. And in the root directory use java com.test.Main.

对于运行此代码,您应该放置Main。类在包中,如目录。/com/test/Main.Java。在根目录中使用java .test. main。

#5


31  

When the same code works on one PC, but it shows the error in another, the best solution I have ever found is compiling like the following:

当相同的代码在一个PC上运行时,但是它显示了另一个PC上的错误,我所找到的最好的解决方案是编译如下:

javac HelloWorld.java
java -cp . HelloWorld

#6


26  

What helped me was specifying the classpath on the command line, for example:

帮助我的是在命令行上指定类路径,例如:

  1. Create a new folder, C:\temp

    创建一个新的文件夹,C:\temp。

  2. Create file Temp.java in C:\temp, with the following class in it:

    在C:\temp中创建文件Temp.java:\temp,其中包含以下类:

    public class Temp {
        public static void main(String args[]) {
            System.out.println(args[0]);
        }
    }
    
  3. Open a command line in folder C:\temp, and write the following command to compile the Temp class:

    打开文件夹C:\temp中的命令行,并编写以下命令来编译temp类:

    javac Temp.java
    
  4. Run the compiled Java class, adding the -classpath option to let JRE know where to find the class:

    运行编译后的Java类,添加-classpath选项,让JRE知道在哪里找到类:

    java -classpath C:\temp Temp Hello!
    

#7


18  

According to the error message ("Could not find or load main class"), there are two categories of problems:

根据错误消息(“无法找到或加载主类”),存在以下两类问题:

  1. Main class could not be found
  2. 无法找到主类。
  3. Main class could not be loaded (this case is not fully discussed in the accepted answer)
  4. 主类无法加载(此情况未在已接受的答案中充分讨论)

Main class could not be found when there is typo or wrong syntax in the fully qualified class name or it does not exist in the provided classpath.

当在完全限定的类名中出现了错误语法或者在提供的类路径中不存在时,无法找到主类。

Main class could not be loaded when the class cannot be initiated, typically the main class extends another class and that class does not exist in the provided classpath.

当类不能被启动时,主类不能被加载,通常,主类扩展另一个类,而该类在提供的类路径中不存在。

For example:

例如:

public class YourMain extends org.apache.camel.spring.Main

If camel-spring is not included, this error will be reported.

如果不包括camel-spring,将会报告此错误。

#8


11  

Sometimes what might be causing the issue has nothing to do with the main class, and I had to find this out the hard way. It was a referenced library that I moved, and it gave me the:

有时引起这个问题的原因与主要的课程无关,而我必须通过困难的方式来解决这个问题。它是我移动的参考图书馆,它给了我:

Could not find or load main class xxx Linux

无法找到或装入主类xxx Linux ?

I just deleted that reference, added it again, and it worked fine again.

我刚刚删除了那个引用,又添加了它,它又恢复正常了。

#9


11  

I had such an error in this case:

在这种情况下,我犯了这样一个错误:

java -cp lib.jar com.mypackage.Main

It works with ; for Windows and : for Unix:

它的工作原理;适用于Windows和:Unix:

java -cp lib.jar; com.mypackage.Main

#10


8  

Try -Xdiag.

-Xdiag试试。

Steve C's answer covers the possible cases nicely, but sometimes to determine whether the class could not be found or loaded might not be that easy. Use java -Xdiag (since JDK 7). This prints out a nice stacktrace which provides a hint to what the message Could not find or load main class message means.

Steve C的回答很好地涵盖了可能的情况,但有时要确定这个类是否被发现或加载可能不是那么容易。使用java -Xdiag(因为JDK 7)。这将打印出一个漂亮的堆栈跟踪,它提供了一个提示,提示消息无法找到或加载主类消息的含义。

For instance, it can point you to other classes used by the main class that could not be found and prevented the main class to be loaded.

例如,它可以指向无法找到的主类使用的其他类,并阻止主类加载。

#11


6  

In this instance you have:

在这个例子中,你有:

Could not find or load main class ?classpath

无法找到或装入主类?类路径?

It's because you are using "-classpath", but the dash is not the same dash used by java on the command prompt. I had this issue copying and pasting from Notepad to cmd.

这是因为您使用的是“-classpath”,但是dash并不是java在命令提示符上使用的相同的破折号。我从记事本复制粘贴到cmd。

#12


6  

Use this command:

使用这个命令:

java -cp . [PACKAGE.]CLASSNAME

Example: If your classname is Hello.class created from Hello.java then use the below command:

例子:如果你的类名是Hello。类创建的你好。然后使用下面的命令:

java -cp . Hello

If your file Hello.java is inside package com.demo then use the below command

你好如果你的文件。java在包内部。演示然后使用下面的命令。

java -cp . com.demo.Hello

With JDK 8 many times it happens that the class file is present in the same folder, but the java command expects classpath and for this reason we add -cp . to take the current folder as reference for classpath.

使用JDK 8很多次,类文件出现在同一个文件夹中,但是java命令期望类路径,因此我们添加-cp。将当前文件夹作为类路径的引用。

#13


5  

In my case, error appeared because I had supplied the source file name instead of the class name.

在我的例子中,出现错误是因为我提供了源文件名而不是类名。

We need to supply the class name containing the main method to the interpreter.

我们需要向解释器提供包含主方法的类名。

#14


4  

“找不到或装入主类”是什么意思?

Class file location: C:\test\com\company

类文件位置:C:\ \ com \公司测试

File Name: Main.class

文件名称:Main.class

Fully qualified class name: com.company.Main

全限定类名:com.company.Main。

Command line command:

命令行命令:

java  -classpath "C:\test" com.company.Main

Note here that class path does NOT include \com\company

注意,类路径不包括\com\company。

#15


4  

This might help you if your case is specifically like mine: as a beginner I also ran into this problem when I tried to run a Java program.

如果你的情况和我的一样,这可能会对你有所帮助:作为一个初学者,我在尝试运行Java程序时也遇到了这个问题。

I compiled it like this:

我这样编译:

javac HelloWorld.java

And I tried to run also with the same extension:

我也试着用同样的扩展来运行:

java Helloworld.java

When I removed the .java and rewrote the command like java HelloWorld, the program ran perfectly. :)

当我删除了.java并重写了像java HelloWorld这样的命令时,程序运行得很好。:)

#16


3  

First set the path using this command;

首先使用此命令设置路径;

set path="paste the set path address"

Then you need to load the program. Type "cd (folder name)" in the stored drive and compile it. For Example, if my program stored on the D drive, type "D:" press enter and type " cd (folder name)".

然后你需要加载程序。在存储驱动器中键入“cd(文件夹名)”并编译它。例如,如果我的程序存储在D驱动器上,键入“D:”按enter并键入“cd(文件夹名)”。

#17


3  

This is a specific case, but since I came to this page looking for a solution and didn't find it, I'll add it here.

这是一个具体的例子,但由于我来到这个页面寻找解决方案,并没有找到它,我将在这里添加它。

Windows (tested with 7) doesn't accept special characters (like á) in class and package names. Linux does, though.

Windows(用7测试)不接受类和包名中的特殊字符(比如a)。虽然Linux,。

I found this out when I built a .jar in NetBeans and tried to run it in command line. It ran in NetBeans but not in command line.

当我在NetBeans中构建一个.jar并试图在命令行运行它时,我发现了这个问题。它在NetBeans中运行,但不在命令行中。

#18


3  

What fixed the problem in my case was:

在我的案例中,解决的问题是:

Right click on the project/class you want to run, then Run As->Run Configurations. Then you should either fix your existing configuration or add new in the following way:

右键单击要运行的项目/类,然后运行As->运行配置。然后,您要么修改现有配置,要么以以下方式添加新配置:

open the Classpath tab, click on the Advanced... button then add bin folder of your project.

打开Classpath选项卡,单击Advanced…按钮然后添加项目的bin文件夹。

#19


3  

If you use Maven to build the JAR file, please make sure to specify the main class in the pom.xml file:

如果您使用Maven构建JAR文件,请确保在pom中指定主类。xml文件:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>class name us.com.test.abc.MyMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

#20


3  

I spent a decent amount of time trying to solve this problem. I thought that I was somehow setting my classpath incorrectly but the problem was that I typed:

我花了相当多的时间来解决这个问题。我认为我的路径设置不正确但问题是我输入了:

java -cp C:/java/MyClasses C:/java/MyClasses/utilities/myapp/Cool  

instead of:

而不是:

java -cp C:/java/MyClasses utilities/myapp/Cool   

I thought the meaning of fully qualified meant to include the full path name instead of the full package name.

我认为完全限定的含义应该包括完整的路径名,而不是完整的包名。

#21


2  

On Windows put .; at the CLASSPATH value in the beginning.

在Windows上把。在开始时的CLASSPATH值。

The . (dot) means "look in the current directory". This is a permanent solution.

的。(dot)表示“查看当前目录”。这是一个永久的解决方案。

Also you can set it "one time" with set CLASSPATH=%CLASSPATH%;.. This will last as long as your cmd window is open.

您还可以将它设置为“一次”,设置类路径=%CLASSPATH%;..这将持续只要您的cmd窗口打开。

#22


2  

All answers here are directed towards Windows users it seems. For Mac, the classpath separator is :, not ;. As an error setting the classpath using ; is not thrown then this can be a difficult to discover if coming from Windows to Mac.

所有的答案都指向Windows用户。对于Mac,类路径分隔符是:,而不是;。作为设置类路径的错误;如果从Windows到Mac,就很难发现这一点。

Here is corresponding Mac command:

以下是对应的Mac命令:

java -classpath ".:./lib/*" com.test.MyClass

Where in this example the package is com.test and a lib folder is also to be included on classpath.

在这个示例中,包是com。测试和lib文件夹也包含在类路径中。

#23


2  

When running the java with the -cp option as advertised in Windows PowerShell you may get an error that looks something like:

当在Windows PowerShell中使用-cp选项运行java时,您可能会得到如下错误:

The term `ClassName` is not recognized as the name of a cmdlet, function, script ...

In order to for PowerShell to accept the command, the arguments of the -cp option must be contained in quotes as in:

为了让PowerShell接受命令,-cp选项的参数必须包含在引号中:

java -cp 'someDependency.jar;.' ClassName

Forming the command this way should allow Java process the classpath arguments correctly.

通过这种方式形成命令,可以正确地允许Java处理类路径参数。

#24


2  

Sometimes, in some online compilers that you might have tried you will get this error if you don't write public class [Classname] but just class [Classname].

有时候,在一些在线编译器中,如果您不编写公共类(类名),而只编写类[Classname],您可能会得到这个错误。

#25


2  

In the context of IDE development (Eclipse, NetBeans or whatever) you have to configure your project properties to have a main class, so that your IDE knows where the main class is located to be executed when you hit "Play".

在IDE开发(Eclipse、NetBeans或其他)的环境中,您必须配置项目属性以拥有一个主类,以便您的IDE知道当您点击“Play”时,将在何处执行主类。

  1. Right click your project, then Properties
  2. 右键单击项目,然后是属性。
  3. Go to the Run category and select your Main Class
  4. 转到Run类别并选择您的主类。
  5. Hit the Run button.
  6. 点击运行按钮。

“找不到或装入主类”是什么意思?

#26


1  

In Java, when you sometimes run the JVM from the command line using the java executable and are trying to start a program from a class file with public static void main (PSVM), you might run into the below error even though the classpath parameter to the JVM is accurate and the class file is present on the classpath:

在Java中,当你有时从命令行运行JVM使用Java可执行文件和试图启动一个项目从一个类文件公共静态孔隙主要(PSVM),你可能会遇到以下错误即使JVM类路径参数是准确和类路径上的类文件存在:

Error: main class not found or loaded

This happens if the class file with PSVM could not be loaded. One possible reason for that is that the class may be implementing an interface or extending another class that is not on the classpath. Normally if a class is not on the classpath, the error thrown indicates as such. But, if the class in use is extended or implemented, java is unable to load the class itself.

如果使用PSVM的类文件无法加载,就会发生这种情况。其中一个可能的原因是,该类可能正在实现一个接口或扩展另一个不在类路径上的类。正常情况下,如果类不在类路径上,则抛出的错误指示为这样。但是,如果使用的类被扩展或实现,java就无法装入类本身。

Reference: https://www.computingnotes.net/java/error-main-class-not-found-or-loaded/

参考:https://www.computingnotes.net/java/error-main-class-not-found-or-loaded/

#27


1  

I got this error after doing mvn eclipse:eclipse This messed up my .classpath file a little bit.

在做了mvn eclipse之后,我得到了这个错误:eclipse把我的.classpath文件弄得一团糟。

Had to change the lines in .classpath from

需要更改.classpath中的行吗?

<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>

to

<classpathentry kind="src" path="src/main/java" output="target/classes" />
<classpathentry kind="src" path="src/main/resources" excluding="**"  output="target/classes" />

#28


1  

You really need to do this from the src folder. There you type the following command line:

您确实需要从src文件夹中执行此操作。在这里输入以下命令行:

[name of the package].[Class Name] [arguments]

Let's say your class is called CommandLine.class, and the code looks like this:

假设你的类叫做命令行。类,代码如下所示:

package com.tutorialspoint.java;

    /**
     * Created by mda21185 on 15-6-2016.
     */

    public class CommandLine {
        public static void main(String args[]){
            for(int i=0; i<args.length; i++){
                System.out.println("args[" + i + "]: " + args[i]);
            }
        }
    }

Then you should cd to the src folder and the command you need to run would look like this:

然后,您应该将cd映射到src文件夹,您需要运行的命令如下所示:

java com.tutorialspoint.java.CommandLine this is a command line 200 -100

And the output on the command line would be:

命令行上的输出是:

args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

#29


1  

In my case, I got the error because I had mixed UPPER- and lower-case package names on a Windows 7 system. Changing the package names to all lower case resolved the issue. Note also that in this scenario, I got no error compiling the .java file into a .class file; it just wouldn't run from the same (sub-sub-sub-) directory.

在我的例子中,我得到了错误,因为我在Windows 7系统上混合了大小写包名。将包名更改为所有小写字母都解决了这个问题。还要注意,在这个场景中,我没有错误地将.java文件编译成.class文件;它不会从相同的(sub-sub-)目录运行。

#30


0  

I was unable to solve this problem with the solutions stated here (although the answer stated has, no doubt, cleared my concepts). I faced this problem two times and each time I have tried different solutions (in the Eclipse IDE).

我无法用此处所述的解决方案来解决这个问题(尽管答案声明无疑地澄清了我的概念)。我两次遇到这个问题,每次尝试不同的解决方案(在Eclipse IDE中)。

  • Firstly, I have come across with multiple main methods in different classes of my project. So, I had deleted the main method from subsequent classes.
  • 首先,我在我的项目的不同类别中遇到了多种主要方法。因此,我已经删除了后续类的主要方法。
  • Secondly, I tried following solution:
    1. Right click on my main project directory.
    2. 右键单击我的主项目目录。
    3. Head to source then clean up and stick with the default settings and on Finish. After some background tasks you will be directed to your main project directory.
    4. 头到源,然后清理和坚持默认设置和完成。在一些后台任务之后,您将被定向到您的主项目目录。
    5. After that I close my project, reopen it, and boom, I finally solved my problem.
    6. 在那之后,我关闭了我的项目,重新打开它,然后,我终于解决了我的问题。
  • 其次,我尝试了以下解决方案:右键单击我的主项目目录。头到源,然后清理和坚持默认设置和完成。在一些后台任务之后,您将被定向到您的主项目目录。在那之后,我关闭了我的项目,重新打开它,然后,我终于解决了我的问题。