本文翻译自: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命令执行以下操作:

  1. Search for the compiled version of the com.acme.example.ListUsers class. 搜索com.acme.example.ListUsers类的编译版本。
  2. Load the class. 加载课程。
  3. Check that the class has a main method with signature , return type and modifiers given by public static void main(String[]) . 检查该类是否具有main方法,该方法具有public static void main(String[])给出的签名返回类型修饰符 (Note, the method argument's name is NOT part of the signature.) (请注意,方法参数的名称不是签名的一部分。)
  4. 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 java command was not able to find the class. java命令无法找到该类。 And indeed, the "..." in the message will be the fully qualified class name that java is looking for. 实际上,消息中的“ ...”将是java正在寻找的标准类名

So why might it be unable to find the class? 那么为什么找不到课程呢?

Reason #1 - you made a mistake with the classname argument 原因#1-您在输入classname参数时犯了一个错误

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 are a variety of wrong ways to specify the class name: (或者...正确的类名,但格式错误。)考虑以上示例,这里有多种错误的方法来指定类名:

  • Example #1 - a simple class name: Example#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; 当在com.acme.example之类的包中声明该类时,则必须在java命令中使用完整的类名, 包括包名; eg 例如

     java com.acme.example.ListUser 
  • Example #2 - a filename or pathname rather than a class name: Example#2-文件名或路径名而不是类名:

     java ListUser.class java com/acme/example/ListUser.class 
  • Example #3 - a class name with the casing incorrect: Example#3-类名的大小写不正确:

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

     java com.acme.example.mistuser 
  • Example #5 - a source filename Example#5-源文件名

     java ListUser.java 
  • Example #6 - you forgot the class name entirely Example#6-您完全忘记了类名

     java lots of arguments 

Reason #2 - the application's classpath is incorrectly specified 原因#2-应用程序的类路径指定不正确

The second likely cause is that the class name is correct, but that the java command cannot find the class. 第二个可能的原因是类名正确,但是java命令找不到该类。 To understand this, you need to understand the concept of the "classpath". 要了解这一点,您需要了解“类路径”的概念。 This is explained well by the Oracle documentation: Oracle文档对此作了很好的解释:

  • The java command documentation java命令文档
  • Setting the Classpath . 设置类路径 。
  • The Java Tutorial - PATH and CLASSPATH Java教程-PATH和CLASSPATH

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.) (是的,请阅读它们。Java程序员至少了解 Java类路径机制的基本原理很重要。)
  2. Look at command line and / or the CLASSPATH environment variable that is in effect when you run the java command. 查看命令行和/或运行java命令时有效的CLASSPATH环境变量。 Check that the directory names and JAR file names are correct. 检查目录名和JAR文件名是否正确。
  3. 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. 如果类路径中有相对路径名,请从运行java命令时有效的当前目录中检查它们是否正确解析...。
  4. Check that the class (mentioned in the error message) can be located on the effective classpath. 检查该类(在错误消息中提到)可以位于有效的类路径上。
  5. Note that the classpath syntax is different for Windows versus Linux and Mac OS. 请注意,Windows与Linux和Mac OS的类路径语法不同 (The classpath separator is ; on Windows and : on the others. If you use the wrong separator for your platform, you won't get an explicit error message. Instead, you will get a nonexistent file or directory on the path that will be silently ignored.) (类路径分隔符;在Windows上:在别人如果使用了错误的分隔符为你的平台,你不会得到一个明确的错误消息,相反,你会得到,这将是该路径上不存在的文件或目录。默默地忽略。)

Reason #2a - the wrong directory is on the classpath 原因#2a-错误的目录位于类路径中

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 . 通过将完全限定的名称映射到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.example.Foon的类时,它将使用该名称查找“ .class”文件路径名:

  /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 原因#2b-子目录路径与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将在目录“ com / acme / example”中查找“ Foon.class”:

  • 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. 如果您尝试通过移动某个类来重命名该类,则该操作也会失败...但是异常stacktrace会有所不同。 It is liable to say something like this: 可能会这样说:

     Caused by: java.lang.NoClassDefFoundError: <path> (wrong name: <name>) 

    because the FQN in the class file doesn't match what the class loader is expecting to find. 因为类文件中的FQN与类加载器期望找到的内容不匹配。

To give a concrete example, supposing that: 举一个具体的例子,假设:

  • you want to run com.acme.example.Foon class, 您想运行com.acme.example.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. 在大多数Java版本中,可以将-classpath选项缩短为-cp Check the respective manual entries for java , javac and so on. 检查javajavac等相应的手册条目。
  • 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 原因#2c-类路径中缺少依赖项

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 (eg 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 原因#3-该类已在错误的包中声明

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. 有时会发生某人将源代码文件放入其源代码树中错误的文件夹中的情况,或者他们忽略了package声明。 If you do this in an IDE, the IDE's compiler will tell you about this immediately. 如果您在IDE中执行此操作,则IDE的编译器会立即告诉您有关此信息。 Similarly if you use a decent Java build tool, the tool will run javac in a way that will detect the problem. 同样,如果您使用不错的Java构建工具,该工具将以检测问题的方式运行javac 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. 但是,如果您手工构建Java代码,则可以通过这种方式进行处理,以使编译器不会注意到问题,并且生成的“ .class”文件不在您期望的位置。

Still can't find the problem? 还是找不到问题?

There lots of things to check, and it is easy to miss something. 有很多东西要检查,很容易错过一些东西。 Try adding the -Xdiag option to the java command line (as the first thing after java ). 尝试将-Xdiag选项添加到java命令行(作为java之后的第一件事)。 It will output various things about class loading, and this may offer you clues as to what the real problem is. 它将输出有关类加载的各种信息,这可能会为您提供真正问题所在的线索。

Also, consider possible problems caused by copying and pasting invisible or non-ASCII characters from websites, documents and so on. 另外,请考虑由网站,文档等复制和粘贴不可见或非ASCII字符引起的可能问题。 And consider "homoglyphs", were two letters or symbols look the same ... but aren't. 考虑“象形文字”,如果两个字母或符号看起来相同……却不相同。


The java -jar <jar file> syntax java -jar <jar file>语法

The alternative syntax used for "executable" JAR files is as follows: 用于“可执行” JAR文件的替代语法如下:

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

eg 例如

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

In this case the name of the entry-point class (ie 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. 典型的Java IDE支持在IDE JVM本身或子JVM中运行Java应用程序。 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. 这些通常不受此特定异常的影响,因为IDE使用其自身的机制来构造运行时类路径,标识主类并创建java命令行。

However it is still possible for this exception to occur, if you do things behind the back of the IDE. 但是,如果您在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. 例如,如果您先前已在Eclipse中为Java应用程序设置了Application Launcher,然后在不通知Eclipse的情况下将包含“ main”类的JAR文件移动到了文件系统中的其他位置,则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可能也很容易混淆。 IDE's are hugely complicated pieces of software comprising many interacting parts. IDE是非常复杂的软件,包含许多交互部分。 Many of these parts adopt various caching strategies in order to make the IDE as a whole responsive. 这些部分中的许多部分都采用了各种缓存策略,以使IDE整体具有响应能力。 These can sometimes go wrong, and one possible symptom is problems when launching applications. 这些有时可能会出错,并且一种可能的症状是启动应用程序时出现问题。 If you suspect this could be happening, it is worth trying things lie restarting your IDE and rebuilding the project. 如果您怀疑这可能发生,那么值得尝试的事情就是重新启动IDE并重建项目。


Other References 其他参考

  • From the Oracle Java Tutorials - Common Problems (and Their Solutions) 来自Oracle Java教程- 常见问题(及其解决方案)

#3楼

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. 我只是删除了该引用,将其再次添加,然后再次正常工作。


#4楼

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. 在存储的驱动器中键入“ cd(文件夹名称)”并进行编译。 For Example, if my program stored on the D drive, type "D:" press enter and type " cd (folder name)". 例如,如果我的程序存储在D驱动器上,则键入“ D:”,然后按Enter键并键入“ cd(文件夹名称)”。


#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. 我们需要将包含main方法的类名提供给解释器。


#6楼

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

“找不到或加载主类”是什么意思?相关推荐

  1. MAC jenkins生成allure报告失败提示:错误:找不到无法加载主类 io.qameta.allure.CommandLine

    最近在学习jenkins工具做持续化集成(CI),遇到了这个问题,没办法,谁叫我是踩坑王-giao! 后来自己在网上找了很多类似的案例,相同的案例几乎没有遇到,后来经过自己持续2天的努力,终于找到了解 ...

  2. 找不到或者无法加载主类

    我们将从HelloWorld程序开始:\n\npublic class HelloWorld {\n\npublic static void main(String[] args) {\n\nSyst ...

  3. java加载找不到类_Java 找不到或无法加载主类的修复方法

    有时,当我们运行Java程序时,我们可能会看到"找不到或无法加载主类".原因很容易猜测:JVM找不到主类并给出了这个错误.但是为什么不能呢? 在本文中,我们将讨论找不到主类的可能原 ...

  4. hadoop错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster

    错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster 原创hongxiao2016 最后发布于2019-03-30 21:20:5 ...

  5. 错误: 找不到或无法加载主类 com.wdg.auditproject.auditproject.action.TestClass

    今天在web项目中创建一个测试的类TestClass,然后在里面写主方法,就像这样: package com.wdg.auditproject.action;public class TestClas ...

  6. java报错-找不到或无法加载主类(Error: Could not find or load main class)

    此文首发于我的个人博客:java报错-找不到或无法加载主类(Error Could not find or load main class) - zhang0peter的个人博客 比如说test.ja ...

  7. java问题:错误: 找不到或无法加载主类 HelloWorld

    有时候我们需要直接用jdk提供的java命令来执行class文件让软件运行起来,特别是很多初学者,但经常会发现如下提示: D:\java\eclipse-workspace\first\src\fir ...

  8. 找不到或无法加载主类 org.jivesoftware.openfire.starter.ServerStarter

    clean项目后出现 找不到或无法加载主类 org.jivesoftware.openfire.starter.ServerStarter 1. 出现上面的错误时,在Ant中重新build.

  9. Error 错误: 找不到或无法加载主类

    jar包问题:项目的Java Build Path中的Libraries中有个jar包的Source attachment指为了一个不可用的jar包,解决办法是:将这个不可用的jar包remove掉. ...

最新文章

  1. “深度学习一点也不难!”
  2. 阿里达摩院已经研发出第一个可控的量子比特
  3. Redis 通用操作1
  4. 类与类之间 相同属性及字段拷贝
  5. 从运维域看 Serverless 真的就是万能银弹吗?
  6. python做大数据的框架_Python+大数据计算平台,PyODPS架构手把手教你搭建
  7. java字符串的哈希码_获取Java中字符串的哈希码
  8. 0分配不到地址_图解 Go 内存分配器
  9. 百度AI语音语义一体化技术 识别的同时进行语义分析
  10. java类的引用使用即String类的toUpperCase的使用
  11. 高程(DEM) ASCII数据获取
  12. thinkphp5基于php的校园微博系统--php-计算机毕业设计
  13. git中提交显示!rejected,如何解决?
  14. IT服务台方案:提供完整的业务流程视图
  15. 配置静态路由——默认路由
  16. 浅析“高内聚,低耦合”
  17. 解决document.form.submit()对象不支持此属性或方法
  18. VScode快捷键配置汇总
  19. BSON与JSON的区别
  20. [Windows]批处理变更用户文件夹到其他位置

热门文章

  1. 海康威视主码流和子码流的区别
  2. idm老是下载到99多就停止了 idm下载中断后无法继续下载
  3. C编程 求1到100之间的奇偶数之和
  4. 计算机管理里边如何分盘,如何给电脑分盘【设置门径】
  5. ICP备案和ICP许可证区别
  6. u盘刻录光盘后空间缩小解决方法
  7. 华科图书情报专硕考研复试与读研
  8. POJ2187-最远点对-旋转卡壳(怎么开心怎么读)
  9. DEMO-lidar原理介绍及数据集运行测试
  10. 语音(识别)处理教程