问题
I'm trying to run my junit test (to verify that a properties file loads correctly) but
I get ClassNotFoundException
although the class is there and all required libraries are there too.
Here it is the error I get :
Class not found ConfigurationManagerTest java.lang.ClassNotFoundException: ConfigurationManagerTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
any ideas on how to fix this ?
Thanks.
回答1:
ConfigurationManagerTest
is not being found on your classpath. Ensure that the ConfigurationManagerTest.class
file is available on your classpath.
It might not exist if it wasn't successfully compiled or if it's being created in a directory that you haven't told the Eclipse project should be on the classpath.
Assuming that you've put your test classes in a separate folder, make sure that it shows up in the "Package Explorer" view (rather than the "Navigator" view).
Under the project properties, "Java Build Path" and the "Source" tab, you can also see if the source folder is included for building as well as where the .class files are generated.
回答2:
There is one more possibility. I had the same problem just now and no one of the solutions here helped. Except removing and recreating of the project - I didn't want to try it. What did help, was to clean the project two times immediately one after another! Clean + build could be repeated any number of times - it won't help. Only clean+clean and after that build goes OK. (Eclipse 3.6). Of course, you should disable autobuild for that.
Edit: This post has got its last plus on 15.11.2017. So, the problem (and the solution) remains actual.
回答3:
Another possible problem is a missing builder (it will prevent from your .class file from being built).
Check that your .project file has the following lines
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
回答4:
This happened to me when I was dragging and dropping test classes to different packages. I just did the following
- Saved the class in a text editor.
- Deleted the offending class from eclipse project explorer.
- Re-created the class.
Bingo! I can now run the test!
回答5:
I had this problem and it was kind of tricky to realise what was wrong. The project had a dependency project with some error, which stopped the build from execute. When I remove this dependency problem, the project was built as expected.
Ps.: I am working on a project that has many compilation errors, because we are porting an application that was converted from Delphi to Java, so I didn't care to the compilation error at the beginning, that's why it took me some time to find out the problem.
回答6:
check properties->java build path -> libraries. there should be no errors, in my case there was errors in the maven. once I put the required jar in the maven repo, it worked fine
回答7:
I tried all the answers described here but none worked, but found this thread where slomek solves the problem in a very easy manner. Just go to project -> properties --> java build path. Then move Junit to the top by hitting the up bottom to the right. Then everything compiles just fine.
回答8:
I tried everything mentioned here and in other posts. Some of the solutions that people proffered were:
- Change the output folder for the test
- Create a custom builder for the project that would run test-compile from Maven
- Move the Maven dependencies higher in the Order and Export list in the project build path
There were many, many more but the one that I found to work was as follows: Close the development environment. Delete the jars used by the project from my local Maven repository. Open the IDE. Build the project. Run the test.
After hours of beating my head against my keyboard and following suggested solutions, this one worked!
回答9:
what worked for me is to remove the runconfiguration of the test. Then right click the testclass and click run as junit test.
now it recreates a correct run config for me.
回答10:
Another way that this can unfold, and just did for me, is if you have a build error that Eclipse doesn't tell you about. If compiling the unit test fails then there is no .class file and you will get the ClassNotFoundException.
In my case there was a missing 3rd party jar file. When I ran the test I got a pop-up window that said "Errors exist in required project(s)". I work with a huge project and I always get that message because some of the source is not available to eclipse (long story). Also it doesn't say what the errors are. So I click "Continue" and then I get the exception.
Once I realized what was going on it was easy to fix by adding the missing jar to the classpath under Run -> Debug Configurations...
I'm not sure how to best detect or prevent this from happening except to be aware of this possibility when something goes wrong and to review your most recent changes for what might have gone wrong.
回答11:
Are you sure your test class is in the build folder? You're invoking junit in a separate JVM (fork=true) so it's possible that working folder would change during that invocation and with build being relative, that may cause a problem.
Run ant from command line (not from Eclipse) with -verbose or -debug switch to see the detailed classpath / working dir junit is being invoked with and post the results back here if you're still can't resolve this issue.
回答12:
A variation on Guy's answer above, involving additional builders. Say you have an Ant builder configured:
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value><project>/.externalToolBuilders/myprojectantlaunch.launch</value>
</dictionary>
</arguments>
</buildCommand>
</buildSpec>
If the Ant build and Eclipse build have differing output locations, Eclipse may not be able to locate the test class on the classpath. In the case of an Ant builder, also check the configured targets for clean, auto, manual and after-clean builds, to ensure that the target which builds the unit tests is called.
回答13:
I add this answer as my solution review from the above.
- You simply edit the file
.project
in the main project folder. Use a proper XML Editor otherwise you will get afatal error
from Eclipse that stats you can not open this project. - I made my project nature
Java
by adding this<nature>org.eclipse.jdt.core.javanature</nature>
to<natures></natures>
. - I then added those lines correctly indented
<buildCommand><name>org.eclipse.jdt.core.javabuilder</name><arguments></arguments></buildCommand>
to<buildSpec></buildSpec>
. Run as JUnit
... Success
回答14:
Right click the project in the Explorer: Build Path -> Order and Export -> Select JRE System Library [jdk] and click Bottom button.
回答15:
Yet another variation.
Somehow, my formerly working test classes appeared to be running from some other location; my edits would not execute when I ran the tests.
I found that the output folder for my ${project_loc}src/test/java files was not what I expected. It had inadvertently been set to ${project_loc}target/classes. I set it properly in project properties, Java Build Path, Source tab.
回答16:
If you have a maven project try to run:
mvn clean compile
and then in eclipse clean & build your project.
回答17:
I had the similar problem with my Eclipse Helios which debugging Junits. My problem was little different as i was able to run Junits successfully but when i was getting ClassNotFoundException while debugging the same JUNITs.
I have tried all sort of different solutions available in Stackoverflow.com and forums elsewhere, but nothing seem to work. After banging my head with these issue for close to two days, finally i figured out the solution to it.
If none of the solutions seem to work, just delete the .metadata folder created in your workspace. This would create an additional overhead of importing the projects and all sorts of configuration you have done, but these will surely solve these issue.
Hope these helps.
回答18:
In "Package Explorer" view, Right click your test class, then "Build Path">>"Include", it should be OK.
回答19:
I too faced the same exception, none of the solutions over internet helped me out. my project contains multiple modules. My Junit code resides in Web module. And it's referring to client module's code.
Finally , I tried : Right click on (Web module) project -->build path--> source tab--> Link source --> added the src files location (Client module's)
Thats it! It worked like a charm Hope it helps
回答20:
I received this error because I had recently created a new workspace and my "Installed JREs" were not set up correctly. Make sure in Preferences -> Java -> Installed JREs
the root folder of your JDK is selected. The default for me in a new workspace was a JRE for some reason.
回答21:
I know this has been already answered a long time ago but the answer didnt fix it for me but, I just removed everything from the sources in the buildpath settings and re-added them
回答22:
I had this problem, in my case the problem relies in the compilation, I am using maven and test classes didn't compile.
I fixed it by doing a maven install (it compiles all the files), also you can check for other reasons, that avoid test to compile like if your "run configurations" is skipping the tests to save time.
回答23:
- Assumming you tried everything reasonable (delete target, refresh, clean, rebuild, maven install...) and that you realized that Eclipse does not behave reasonable...
Now: Check if there is no errors suggesting it can be a plugin (any plugin) that has something to do with tests.
In my case:
- I removed EclEmma.
- It started working...
Spent few hours on trying to guess it!.
回答24:
Please make sure the output folder in Java Build Path
tab set as like below,which would determine where the .class file are generated.Then clean the project.
来源:https://stackoverflow.com/questions/5716308/junit-test-class-in-eclipse-java-lang-classnotfoundexception