Eclipse 中奇怪的“java.lang.NoClassDefFoundError”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2233656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:03:00  来源:igfitidea点击:

Strange "java.lang.NoClassDefFoundError" in Eclipse

javaeclipse

提问by Anto

I have a Java project in Eclipse perfectly running smoothly until this afternoon, when I updated some files (including a ant build.xml file). When I build the project, the following error appears:

我在 Eclipse 中有一个 Java 项目,一直运行得很顺利,直到今天下午我更新了一些文件(包括一个 ant build.xml 文件)。当我构建项目时,出现以下错误:

java.lang.NoClassDefFoundError: proj/absa/FrontEnd/ApplicationStarter
Caused by: java.lang.ClassNotFoundException: proj.absa.FrontEnd.ApplicationStarter
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source) at
    java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)  
Exception in thread "main" 

Does anyone have a clue where the problem may be?

有没有人知道问题可能出在哪里?

回答by duffymo

java.lang.ClassNotFoundException means CLASSPATH issues. Not having a clue implies that you're assuming that the CLASSPATH is set properly, but it's not.

java.lang.ClassNotFoundException 表示 CLASSPATH 问题。没有线索意味着您假设 CLASSPATH 设置正确,但事实并非如此。

If you're building with Eclipse, make sure that the directory where your compiled .class files exists, is in the CLASSPATH, and has the necessary .class files in it.

如果您使用 Eclipse 进行构建,请确保您编译的 .class 文件所在的目录位于 CLASSPATH 中,并且其中包含必要的 .class 文件。

If you're building with Ant, make sure you see something like this in your build.xml:

如果您使用 Ant 构建,请确保您在 build.xml 中看到如下内容:

<path id="production.class.path">
    <pathelement location="${production.classes}"/>
    <pathelement location="${production.resources}"/>
    <fileset dir="${production.lib}">
        <include name="**/*.jar"/>
        <exclude name="**/junit*.jar"/>
        <exclude name="**/*test*.jar"/>
    </fileset>
</path>

UPDATE: Either you don't have JAVA_HOME/bin in your PATH or you downloaded the JRE instead of the JDK. Check the directory where you installed Java and see if your /bin directory has javac.exe in it. If you don't have a /bin, download the JDK and install it.

更新:要么您的 PATH 中没有 JAVA_HOME/bin,要么您下载的是 JRE 而不是 JDK。检查您安装 Java 的目录,看看您的 /bin 目录中是否有 javac.exe。如果您没有 /bin,请下载 JDK 并安装它。

If you do have a /bin with javac.exe in it, check your PATH to see that the Java /bin directory is in the PATH. Once you've set it, open a command shell and type "javac -version" to ensure that all is well.

如果您确实有一个带有 javac.exe 的 /bin,请检查您的 PATH 以查看 Java /bin 目录是否在 PATH 中。设置完成后,打开命令外壳并键入“javac -version”以确保一切正常。

What JDK did you tell Eclipse to use?

你告诉 Eclipse 使用什么 JDK?

回答by sreejith

both the problem will be because of classpathref="master-classpath", please check the value is correct

这两个问题都是因为 classpathref="master-classpath",请检查值是否正确

回答by Steve De Caux

I'm seeing this a bit too often lately. Just today I had the issue with a class in the same package as the affected (red-flagged) class !

我最近看到这个有点太频繁了。就在今天,我遇到了与受影响(红旗)班级在同一个包中的班级问题!

Exiting eclipse and restarting generally works to resolve the red flag on the affected class but sometimes a red flag is left on the project, then I also need to close the project and reopen it as well to get rid of the standalone red flag. It looks quite weird to see a red flag on a project, with no red flags in any of its child directories.

退出 eclipse 并重新启动通常可以解决受影响类上的红旗,但有时项目上会留下红旗,然后我还需要关闭项目并重新打开它以消除独立的红旗。在一个项目上看到一个红旗,在它的任何子目录中都没有红旗,这看起来很奇怪。

With maven project clusters, I close and open all of the projects in the cluster after restarting eclipse.

使用 maven 项目集群,我在重启 eclipse 后关闭并打开集群中的所有项目。

回答by NooberOverloard

This seems to be a common error. The solution is to:

这似乎是一个常见的错误。解决办法是:

  • right-click project
  • choose properties
  • choose 'Java Compiler'
  • unclick the first box saying 'Enable project specific settings'
  • apply
  • save
  • run
  • 右键单击项目
  • 选择属性
  • 选择“Java编译器”
  • 取消单击“启用项目特定设置”的第一个框
  • 申请
  • 节省

Hope this helps in some cases.

希望这在某些情况下有所帮助。

回答by Alexandra

While this is a wild guess and may not be applicable in your specific situation, this could've saved me an hour or so.

虽然这是一个疯狂的猜测,可能不适用于您的特定情况,但这本可以为我节省一个小时左右的时间。

In case you have "converted" a plain project into Java project (by editing .project file and adding appropriate tag), make sure you also have a proper specified - my project didn't get built even though Eclipse attempted to and run no builders (success!):

如果您已将一个普通项目“转换”为 Java 项目(通过编辑 .project 文件并添加适当的标签),请确保您也有一个正确的指定 - 即使 Eclipse 尝试并没有运行任何构建器,我的项目也没有构建(成功!):

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>

回答by Puja

I too was facing the same issue. Then discovered that the path for the lib folder in the classpath was not set properly.

我也面临同样的问题。然后发现classpath中lib文件夹的路径没有设置好。

回答by Mike K.

Stab in the dark, but I was having the same exact issue. I solved it by deleting the project from my workspace (be careful not to delete from disk), and then re-imported the project and it worked fine. I think mine was caused by a bad windows shutdown (restarting windows correctly did not correct the issue). HTH.

在黑暗中刺伤,但我遇到了同样的问题。我通过从我的工作区中删除项目来解决它(注意不要从磁盘中删除),然后重新导入该项目并且它工作正常。我认为我的是由错误的 Windows 关闭引起的(正确重新启动 Windows 没有解决问题)。哈。

回答by rmtheis

Doing Project->Clean… in Eclipse solved this problem for me.

在 Eclipse 中执行 Project->Clean... 为我解决了这个问题。

回答by Martin

When you launch your program, it stores the launch configuration that you may latter modify. If something changed in your build/run process, you may have wrong settings. For example I used to work with maven, and some launch configuration reference a maven2_classpath_container. When deleting the launch configuration and running the program again, it can work again.

当您启动程序时,它会存储您以后可能会修改的启动配置。如果在您的构建/运行过程中发生了某些变化,您的设置可能有误。例如我曾经使用 maven,并且一些启动配置引用了 maven2_classpath_container。当删除启动配置并再次运行程序时,它可以再次工作。

回答by Scott Izu

As mentioned above, "java.lang.ClassNotFoundException means CLASSPATH issues."

如上所述,“java.lang.ClassNotFoundException 意味着 CLASSPATH 问题。”

In my setup, I am running Maven to build (instead of Ant) and using Eclipse (instead of Netbeans).

在我的设置中,我运行 Maven 来构建(而不是 Ant)并使用 Eclipse(而不是 Netbeans)。

Usually, to build and setup the project, I will run 'mvn clean', 'mvn compile', 'mvn eclipse:eclipse' from the Windows command prompt. The last command 'mvn eclipse:eclipse' updates the project configuration creating .classpath and .project files.

通常,为了构建和设置项目,我将从 Windows 命令提示符运行“mvn clean”、“mvn compile”、“mvn eclipse:eclipse”。最后一个命令 'mvn eclipse:eclipse' 更新项目配置,创建 .classpath 和 .project 文件。

To fix the problem, I deleted the two files (.classpath and .project) then re-ran the three commands.

为了解决这个问题,我删除了两个文件(.classpath 和 .project),然后重新运行了三个命令。

So depending on your configuration, try to find the classpath/project files (make a backup) and delete them. You can also try deleting the target/release/build folder (whatever is created from the build command) as well. Then try to build/package/configure your project again.

因此,根据您的配置,尝试查找类路径/项目文件(进行备份)并删除它们。您也可以尝试删除 target/release/build 文件夹(无论是从 build 命令创建的)。然后再次尝试构建/打包/配置您的项目。