Eclipse - java.lang.ClassNotFoundException

When I try to run JUnit-Test from Eclipse, I get a "ClassNotFoundException". When you run "mvn test" from the console, everything works fine. In addition, there is no problem with Eclipse.

My project structure is as follows:

  • parent project (pom-packaging)
    • Web project (military packaging - my JUnit test is here)
    • Flex Project
    • Configuration project



edit: How can a class not be found? This is a simple HelloWorld application without special libraries.

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




Testclass (but as I said, it does not work with simple HelloWorld either ...):

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("", "")); } } 
+94
java eclipse exception maven-2
Jun 27 '09 at 15:01
source share
37 answers
  • one
  • 2

I came across this situation several times, and after many attempts, I found a solution.

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

For example, your src/main/java web project should have target/classes as part of the web project, test classes should have target/test-classes also as part of the web project, etc.

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

One more tip if your web project tests require some configuration files under resources, be sure to include this folder as the source folder and create the correct build path configuration.

Hope this helps.

+192
Jun 30 '09 at 8:27
source share

Carlos's approach helped! Eclipse - java.lang.ClassNotFoundException

Try checking the path to the junit startup configuration classes:

  • Open launch configuration
  • Click on jUnit-Test you want to run
  • go to the class path tab
  • Try adding a folder (click on user entries, click on advanced, click "Add folders", click "OK" and find the output folder for the test classes (those that you will find in projektproperties java build path, source))

works for me.

+46
Apr 06 '11 at 12:21
source share

your path to the assembly class is correct, so you can compile. you need to check the path class for your JUnit. go to the "Run" menu and select "Open Launch Dialog". there you will see a tree on the left with JUnit as an option. open this node and find and select your test. in the right pane you will see a tab for the class path. look to make sure your class that is trying to execute the test instance is found.

change

this is apparently a problem with maven and its behavior after release changed the default output folders of Eclipse. I saw the solutions described where

  • placing maven in bootclasspath ABOVE that jre works, or
  • running mvn clean test does the trick or
  • upgrade all your eclipse projects, and the recovery will be fixed.
  • going to your project and choosing Maven-> Update Configuration solves the problem

with the first three, there were reports of a recurring problem. the latter looks best to me, but if it doesn't work, try others.

here and here there is some information

+16
Jun 27 '09 at 15:12
source share

The problem may not be in the class file in your assembly. One solution is a clean project and its restoration.

+4
Aug 03 2018-12-12T00:
source share

There are many confusing suggestions here.

I ran into this problem several times with Maven projects after moving resources by dragging and dropping "n" or refactoring class names.

If this happens, simply copy (do not move) the test case ( .java ) through the terminal / file browser to another location, right-click -> Delete in Eclipse and select delete on disk when the option is provided, move / copy the copied file to the original file location, then select the project in Eclipse and press F5 to update the resources.

This is quick and easy to do, and I constantly fixed the problem for me every time.

+3
Sep 10
source share

I had the same problem, but I understood it! Go to the project file and right-click on it, then click Refresh or press F5. Then try to run it. If it still doesn't work, just forget about it, as I had the EXACT problem, and that just means that the version of Eclipse is trash.

+3
Sep 19 '12 at 10:40
source share

That was my solution to the problem. Of course, many things can lead to its occurrence. For me it was that Maven2 (and not the Eclipse plugin) installed the eclipse profile to use another builder (aspectJ), but I did not have the plugin in eclipse./

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

Cheers Ramon Buckland

+2
Sep 12 '09 at 23:39
source share

Sachin on the right: Even with the correct path to the template, the Problems tab will show that some dependency or resource / project has an error that needs to be fixed so that maven will automatically create and create classes when creating or making changes to your test class.

"Hello,

Its a very old July (that year), but I had the same problem.

The actual problem found that eclipse was not able to generate the class file for the java file, the classpath was correct.

See the problems tab and check if something / file is missing in your project. you can create a new proj and add files one by one and create them until you stop compiling and creating classes (check the workspaces / proj / bin / package / folder for classes)

its strange but true, ecplise failed to complicate because 4 out of 20 java files used one image that was missing. and as a result, not one of the java files was compiled.

CLASSPATH is not a problem here.

+2
Apr 05 2018-11-11T00:
source share

We had an exact exception (using SpringSource Tools, tomcat, on Win7), and the reason is that we redesigned the file name (renamed file) from SubDomain.java to Subdomain.java (D vs d) and somehow collided although SpringSource showed the new name Subdomain.java. The solution was to delete the file (via SpringSource) and create it again under the name Subdomain.java and copy its previous contents. Just like that.

+2
May 4 '12 at 11:01
source share

I decide the path is Bulit ---> libraries ---> add library ---> Junit check junit4

+2
Oct 08 '16 at 6:38
source share

click project->properties->Java build path->Source and verify that each src folder still exists or has been recently deleted. Correct any missing path or wrong path and rebuild and run the test. This will fix the problem.

+1
Mar 14 2018-12-14T00:
source share

All I did was Properties -> Java Build Path -> Order and Export -> Enabled all unchecked boxes -> moved Junit all the way

+1
Apr 04 '13 at 13:27
source share

I tried

 Link : [here][1] Open your run configurations Click on the jUnit-Test you want to start go to the classpath tab 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)) 

worked after

Maven 2 LifeCycle → test

+1
Apr 09 '13 at 19:55
source share

I tried all the solutions on this page: update the project, rebuild, clean up all the projects, restart Eclipse, re-import (even) the projects, rebuild the maven and update. Nothing succeeded. That the job copied the class into a new name that works fine is bizarre, but true.

After suffering this for a while, I just fixed it:

  • Via the Run menu
  • Choose Run Configurations
  • Select the launch configuration associated with your unit test.
  • Deleting an entry from Run Configuration by clicking delete or by clicking on the red X.

Something must be related to the configuration of the cached startup.

+1
Apr 11 '13 at 23:11
source share

Removing the project from eclipse (not from the hard drive), which to some extent cleans up the workspace, and moving the project to eclipse again worked for me again.

+1
Oct 20
source share

Typically, this problem occurs when you run the java Java application tool, which cannot find the class file.

Mostly in the maven project, we see this problem due to the Eclipse-Maven synchronization problem . To solve this problem: Maven-> Update configuration

+1
Jan 04 '18 at 4:09
source share

Have you tried right-clicking on the project root by selecting “properties” and making sure that CLASSPATH is correct? If I remember correctly how you do it.

Anything about how Eclipse runs unit tests that require you to add the Junit JAR to the CLASSPATH in a special way?

I use IntelliJ, so I do not have these problems.

I would check Eclipse myself, but I prefer not to have it on my desktop.

0
Jun 27 '09 at 16:26
source share

Hmm, it looks a little strange, 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 handle this.

Make sure that you also automatically enable the build function. If you want your test classes to be compiled, clean the Maven target folder (and any bin folder that Eclipse can use) correctly. You are also using m2eclipse, as I find this a bit problematic.

0
Jun 27 '09 at 18:08
source share

A solution to my problem that looked like: libs were invalid. If you look in the project's .classpath file, you will see classpathentry tags with the key / value kind = "lib". Some of mine were wrong.

I did not find this until I turned off the verification options. That is, there were so many errors in the JSP files, etc., that the errors in the classpath were not obvious (or perhaps even displayed). As a result, nothing was compiled to the destination output folders, but no useful errors were why.

0
Aug 13 '10 at 21:35
source share

I was also struck by this problem, and I managed to find a sufficient solution for my case. If your Eclipse project has a .classpath file in the root of your project (see It in the Navigator view and not in the package explorer view), make sure your Maven classpathentry appears before your JRE Container classpathentry class.

 <?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 the Java Build Path project to switch Order and Export. If your project has a .classpath file and you only change the order in the Java build path, you will see that the order is not affected and the problem will continue.

And project-> clean will never hurt things after making changes.

0
Feb 17 2018-11-11T00:
source share

Make sure your test run configuration does NOT contain the following lines, OR try turning on automatic Maven dependency management.

 <stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.maven.ide.eclipse.launchconfig.classpathProvider"/> <stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.maven.ide.eclipse.launchconfig.sourcepathProvider"/> 
0
Jul 06 2018-11-11T00:
source share

Specify the correct JDK from Windows> Preferences> Java> Installed JRE.

Do not point to jre, point to the correct JDK. I pointed to JDK 1.6U29 and updated the project.

In the future, the problem will disappear, and jUnit tests work fine.

Thank,
-Tapas

0
Jun 27 '12 at 5:20
source share

I recently encountered the same error in Eclipse, that is, the Eclipse IDE was unable to find the Unit test class no matter how I modify the configurations. Studying previous posts here and on other websites, I double-checked and triple checked the class path and source information and moved up and down the source folder and libraries in both “Run Configuration” and “Java Build Path” config windows ", and I also cleaned up the project and rebuilt it, but none of the tricks work for me. A specific Java project is an old compiled ANT project and contains many jars included in the Eclipse library.

Then I changed the Unit test class to add the main () method, and right-click on the “Run As” Java application instead of the JUnit test, and suddenly Eclipse seems to wake up and correctly identify the class. Subsequently, I switched it back to the Unit test application, and it still works.

This seems like a bug in Eclipse, I assume that a large number of libraries (> 260) may confuse the ability of the JVM to find my JUnit class.

0
Jan 26 '13 at 1:07 on
source share

I tried everything that I read in this long post, and, incredibly, it worked for me, instead of clicking on the test class and choosing Run as JUnit test , clicking on the test method and working like a JUnit test . I have no idea why?

0
Sep 10 '13 at 15:30
source share

The JUnit test from internal eclipse also gave me NoClassDefFoundError. Running "mvn clean test" from the command line gave me the following error on several jars: invalid LOC header (bad signature) Removing these jars from the local m2 storage and running mvn clean test again solved my problem.

0
Nov 27 '13 at 10:17
source share

I had the same problem. All i have done

I am). Created Eclipse Artifacts

mvn clean eclipse:eclipse

II). Update the project and repeat the junit test. Should work fine.

0
Apr 18 '14 at 21:45
source share

Also DOUBLE-CHECK eclipse "Build Web Deployment."

This can be found: Project Properties-> Deployment Assembly.

I recently had an eclipse plugin that modified one of my web projects and added ~ mysteriously ~ added maven / src / test / java, / src / test / resources test directories to the deployment build. UGGGG !!!

That's why my project worked perfectly when I built and deployed only direct maven for tomcat, not ClassNotFoundExceptions ... However, when I did the deployment via Eclipse, Whammo !! I'm starting to get ClassNotFoundExceptions because TestCode is being deployed.

Eric

0
Oct. 20 '14 at 16:41
source share

This means that your pom.xml has unresolved issues. Open the solution to the problem solve accordingly. Then you can successfully run test cases without encountering a classnotfoundexception exception.

0
Feb 17 '15 at 12:45
source share

Reordering the classpath artifacts in the Java build path resolved this for me.

  • Right-click on the project and go to the path to create the project.
  • Go to the Order and Export tab and move the JRE system library after the sources.

This should fix it.

0
Jul 21 '15 at 15:31
source share

JUnit 4.4 is not supported by JMockit / JUnit integration. Only supported are version 4.5 or later.

0
Dec 10 '15 at 14:25
source share
  • one
  • 2



All Articles