junit gives error java.lang.NoClassDefFoundError: junit/framework/JUnit4TestAdapterCache

主宰稳场 提交于 2020-01-03 16:53:37

问题


I'm trying to run a simple unit test to my project at https://github.com/cdwijayarathna/oj4j.

I added both junit-4.12.jar and ant-junit4.jar to the lib folder.

But when I ran "rake run_tests", I'm getting following error at the report location I have given.

java.lang.NoClassDefFoundError: junit/framework/JUnit4TestAdapterCache
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

What's the reason for that? how can I solve this?


回答1:


I had the same exception running junit through ant on Debian. By including the junit4.jar in the $CLASSPATH environment variable I was able to get it running. Which means the jvm was not picking up on the junit4.jar. The /usr/share/java/junit4.jar path is specific to Debian's junit package, so you will need to replace as appropriate.

    $ export CLASSPATH=/usr/share/java/junit4.jar
    $ ant test



回答2:


Set ANT_HOME your $CLASSPATH environment variables.




回答3:


I use specific jars to run tests on a project, here is the command line which solve this issue:

$ CLASSPATH=lib/hamcrest-core-1.3.jar:lib/junit-4.12.jar ant my-junit-target

I've wrote a small shell to encapsulate this command.



来源:https://stackoverflow.com/questions/29878517/junit-gives-error-java-lang-noclassdeffounderror-junit-framework-junit4testadap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!