Eclipse - java.lang.ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1052978/
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-11 22:49:49  来源:igfitidea点击:

Eclipse - java.lang.ClassNotFoundException

javaeclipseexceptionmaven-2

提问by swalkner

When trying to start my JUnit-Test out of Eclipse, I get a "ClassNotFoundException". When running "mvn test" from console - everything works fine. Also, there are no problems reported in Eclipse.

尝试从 Eclipse 启动 JUnit-Test 时,出现“ClassNotFoundException”。从控制台运行“mvn test”时 - 一切正常。此外,Eclipse 中没有报告任何问题。

My project structure is the following:

我的项目结构如下:

  • parent project (pom-packaging)
    • Web project (war-packaging - my JUnit-test is in here)
    • Flex project
    • Configuration project
  • 父项目(pom-packaging)
    • Web 项目(战争包装 - 我的 JUnit 测试在这里)
    • 弹性项目
    • 配置项目


edit: How can the class not be found? It's a simple HelloWorld-Application with no special libraries.

编辑:怎么找不到班级?这是一个简单的 HelloWorld 应用程序,没有特殊的库。

Here's my JUnit's run-configuration: alt text http://www.walkner.biz/_temp/runconfig.png

这是我的 JUnit 的运行配置: 替代文本 http://www.walkner.biz/_temp/runco​​nfig.png



Testclass (but as I said; it doesn't work with a simple HelloWorld either...):

测试类(但正如我所说;它也不适用于简单的 HelloWorld ......):

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import biz.prognoserechnung.domain.User;
import biz.prognoserechnung.domain.UserRepository;
import biz.prognoserechnung.domain.hibernate.UserHibernateDao;

public class UserDaoTest {
/**
 * the applicationcontext.
 */
private ApplicationContext ctx = null;

/**
 * the user itself.
 */
private User record = null;

/**
 * Interface for the user.
 */
private UserRepository dao = null;

@Before
public void setUp() throws Exception {
String[] paths = { "WEB-INF/applicationContext.xml" };
ctx = new ClassPathXmlApplicationContext(paths);
dao = (UserHibernateDao) ctx.getBean("userRepository");
}

@After
public void tearDown() throws Exception {
dao = null;
}

@Test
public final void testIsUser() throws Exception {
Assert.assertTrue(dao.isUser("John", "Doe"));
}

@Test
    public final void testIsNoUser() throws Exception {
    Assert.assertFalse(dao.isUser("not", "existing"));
        Assert.assertFalse(dao.isUser(null, null));
        Assert.assertFalse(dao.isUser("", ""));
    }
}

采纳答案by Carlos

I've come across that situation several times and, after a lot of attempts, I found the solution.

我多次遇到这种情况,经过多次尝试,我找到了解决方案。

Check your project build-path and enable specific output folders for each folder.Go one by one though each source-folder of your project and set the output folder that maven would use.

检查您的项目构建路径并为每个文件夹启用特定的输出文件夹。通过项目的每个源文件夹一个接一个,并设置 maven 将使用的输出文件夹。

For example, your web project's src/main/javashould have target/classesunder the web project, test classes should have target/test-classesalso under the web project and so.

例如,你的web项目src/main/java应该target/classes在web项目下,测试类target/test-classes也应该在web项目下等等。

Using this configuration will allow you to execute unit tests in eclipse.

使用此配置将允许您在 eclipse 中执行单元测试。

Just one more advice, if your web project's tests require some configuration files that are under the resources, be sure to include that folder as a source folder and to make the proper build-path configuration.

还有一个建议,如果您的 web 项目的测试需要一些资源下的配置文件,请确保将该文件夹作为源文件夹包含并进行正确的构建路径配置。

Hope it helps.

希望能帮助到你。

回答by akf

your build classpath is correct, which is why you can compile. the classpath for your JUnit needs to be checked. go to the Run menu and choose 'open run dialog.' in there you should see a tree on the left with JUnit as an option. open that node and find and select your test. on the right pane you will see a tab for classpath. take a look to ensure that your class that the test is trying to instantiate would be found.

您的构建类路径是正确的,这就是您可以编译的原因。需要检查 JUnit 的类路径。转到“运行”菜单并选择“打开运行对话框”。在那里,您应该会在左侧看到一棵带有 JUnit 选项的树。打开该节点并找到并选择您的测试。在右侧窗格中,您将看到类路径选项卡。查看以确保您的测试试图实例化的类会被找到。

edit:

编辑:

this seems to be an issue with maven and its behavior after a release changed the default Eclipse output folders. i have seen solutions described where

这似乎是 maven 及其在发布更改默认 Eclipse 输出文件夹后的行为的问题。我已经看到描述的解决方案在哪里

  • placing maven into the bootclasspath ABOVE the jre works, or
  • running mvn clean testdoes the trick or
  • refreshing all of your eclipse projects, causing a rebuild fixes the problem
  • going to your project and selecting Maven->Update Configuration solve the problem
  • 将 maven 放入 jre 工程上方的引导类路径,或
  • 跑步mvn clean test可以解决问题或
  • 刷新所有 eclipse 项目,导致重建解决问题
  • 转到您的项目并选择 Maven->Update Configuration 解决问题

with the first three, there were reports of the issue recurring. the last looks best to me, but if it doesnt work, please try the others.

对于前三个,有报告称该问题反复出现。最后一个对我来说最好,但如果它不起作用,请尝试其他的。

hereand hereis some info

这里这里的一些信息

回答by duffymo

Have you tried right clicking on your project root, selecting "properties", and making sure that the CLASSPATH is correct? If I recall correctly, that's how you do it.

您是否尝试过右键单击您的项目根目录,选择“属性”,并确保 CLASSPATH 是正确的?如果我没记错的话,你就是这样做的。

Anything about the way Eclipse runs unit tests that requires you to add the junit JAR to the runtime CLASSPATH in a special way?

Eclipse 运行单元测试的方式需要您以特殊方式将junit JAR 添加到运行时CLASSPATH 中吗?

I use IntelliJ, so I don't have these issues.

我使用 IntelliJ,所以我没有这些问题。

I'd check Eclipse myself, but I prefer not having it on my desktop.

我会自己检查 Eclipse,但我更喜欢不在我的桌面上安装它。

回答by Jon

Hmm, looks a little bizarre, try running it with the following annotation at the top of the class:

嗯,看起来有点奇怪,尝试在类的顶部使用以下注释运行它:

@RunWith(SpringJUnit4ClassRunner.class)
public class UserDaoTest {
}

and let me know how you get on with it.

让我知道你是怎么做的。

Check that you have build automatically enabled as well. If you want to make sure your test classes are being compiled correctly clear out the Maven target folder (and any bin folder that Eclipse may be using). Are you using m2eclipse as well, as I find it to be a little problematic.

检查您是否也自动启用了构建。如果您想确保您的测试类被正确编译,请清除 Maven 目标文件夹(以及 Eclipse 可能正在使用的任何 bin 文件夹)。您是否也在使用 m2eclipse,因为我发现它有点问题。

回答by Carlos

This was my solution to the problem. Of course, many things can cause it to occur. For me it was that Maven2 (not the plugin for Eclipse) was setting the eclipse profile up to use a different builder (aspectJ) but I did not have the plugin in eclipse./

这是我解决问题的方法。当然,很多事情都会导致它发生。对我来说,Maven2(不是 Eclipse 的插件)正在设置 Eclipse 配置文件以使用不同的构建器(aspectJ),但我在 Eclipse 中没有该插件。/

http://rbtech.blogspot.com/2009/09/eclipse-galileo-javalangclassnotfoundex.html

http://rbtech.blogspot.com/2009/09/eclipse-galileo-javalangclassnotfoundex.html

Cheers Ramon Buckland

干杯拉蒙巴克兰

回答by Sachin

Its very Old Jul (which year) but I had the same problem .

它非常古老的 7 月(哪一年),但我遇到了同样的问题。

Actual issue found that eclipse was not able to generate class file for the java file , classpath was proper.

实际问题发现eclipse无法为java文件生成class文件,classpath正确。

See the problem tab and check if your project is missing something/file. you can create a new proj and add files one by one and build them until it stops compiling and creating classes ( check the workspace/proj/bin/package/ folder for classes )

查看问题选项卡并检查您的项目是否缺少某些内容/文件。您可以创建一个新的 proj 并一个一个添加文件并构建它们,直到它停止编译和创建类(检查工作空间/proj/bin/package/文件夹中的类)

its wierd but true , ecplise was failing in compliation because 4 of 20 java files were using a single image which was missing. and as result none of the java file was compiled .

它奇怪但真实,ecplise 编译失败,因为 20 个 java 文件中有 4 个使用了丢失的单个图像。结果没有编译任何java文件。

CLASSPATH is not a issue here.

CLASSPATH 在这里不是问题。

回答by Mike

The solution to my problem which was similar: the libs were invalid. If you look in the .classpath file of the project, you'll see classpathentry tags with the key/value kind="lib". Some of mine were incorrect.

我的问题的解决方案类似:库无效。如果您查看项目的 .classpath 文件,您将看到带有键/值 kind="lib" 的 classpathentry 标记。我的一些是不正确的。

I didn't discover this until I turned off Validation settings. That is, there were so many errors in the JSP files, etc, that the classpath errors weren't evident (or possibly even showing up). As a result, nothing was being compiled into the destination output folders, but no helpful errors on why.

直到我关闭验证设置我才发现这一点。也就是说,JSP 文件等中有太多错误,以至于类路径错误并不明显(甚至可能出现)。结果,没有任何内容被编译到目标输出文件夹中,但没有关于原因的有用错误。

回答by cmh

I was hit with this issue also and was able to come up with a sufficient solution for my case. If your Eclipse project has a .classpath file in your project root (see it in Navigator view instead of Package Explorer view), be sure that your Maven classpathentry appears prior to your JRE Container classpathentry.

我也遇到了这个问题,并且能够为我的案例提出足够的解决方案。如果您的 Eclipse 项目在您的项目根目录中有一个 .classpath 文件(在导航器视图而不是包资源管理器视图中查看它),请确保您的 Maven 类路径出现在您的 JRE 容器类路径之前。

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>

If your project does not have a .classpath file, you can edit your project's Java Build Path to switch the Order and Export. If your project has the .classpath file and you only change your ordering in the Java Build Path, you will see that the ordering is not impacted and the issue will continue to occur.

如果您的项目没有 .classpath 文件,您可以编辑项目的 Java Build Path 以切换 Order 和 Export。如果您的项目具有 .classpath 文件并且您只更改了 Java Build Path 中的排序,您将看到排序没有受到影响并且问题将继续发生。

And a project->clean never hurts things after you make the change.

在您进行更改后,项目->清理永远不会伤害任何事情。

回答by kisna

Sachin's right: Even with correct class path, the problems tab will show that some dependency or the Resource/project has error that needs to be fixed in order for maven to automatically build and create classes when you create or make a change in your test class.

Sachin 是对的:即使使用正确的类路径,问题选项卡也会显示某些依赖项或资源/项目存在需要修复的错误,以便 maven 在您创建或更改测试类时自动构建和创建类.

"Hi,

“你好,

Its very Old Jul (which year) but I had the same problem .

它非常古老的 7 月(哪一年),但我遇到了同样的问题。

Actual issue found that eclipse was not able to generate class file for the java file , classpath was proper.

实际问题发现eclipse无法为java文件生成class文件,classpath正确。

See the problem tab and check if your project is missing something/file. you can create a new proj and add files one by one and build them until it stops compiling and creating classes ( check the workspace/proj/bin/package/ folder for classes )

查看问题选项卡并检查您的项目是否缺少某些内容/文件。您可以创建一个新的 proj 并一个一个添加文件并构建它们,直到它停止编译和创建类(检查工作空间/proj/bin/package/文件夹中的类)

its wierd but true , ecplise was failing in compliation because 4 of 20 java files were using a single image which was missing. and as result none of the java file was compiled .

它奇怪但真实,ecplise 编译失败,因为 20 个 java 文件中有 4 个使用了丢失的单个图像。结果没有编译任何java文件。

CLASSPATH is not a issue here."

CLASSPATH 在这里不是问题。”

回答by CoffeJunky

Carlos approach helped! Eclipse - java.lang.ClassNotFoundException

卡洛斯的方法有帮助! Eclipse - java.lang.ClassNotFoundException

Try to check the classpath of the junit run configuration:

尝试检查junit运行配置的类路径:

  1. Open your run configurations
  2. Click on the jUnit-Test you want to start
  3. go to the classpath tab
  4. Try to add a folder (click on user entries, click on advanced, click on add folders, click on ok and search the outputfolder for your test classes(those you find under projektproperties java build path, source))
  1. 打开您的运行配置
  2. 单击要启动的 jUnit-Test
  3. 转到类路径选项卡
  4. 尝试添加一个文件夹(单击用户条目,单击高级,单击添加文件夹,单击确定并在输出文件夹中搜索您的测试类(您在 projektproperties java 构建路径下找到的那些,源))

works for me.

为我工作。