4 Replies Latest reply on Nov 27, 2013 11:25 AM by metalhamster

    How can I solve NoClassDefFoundError

    metalhamster

      Hi everyone,

       

       

      Before I tell you what my problem is, I think it's better to briefly explain how my project is structured.

       

      My project has the following structure:

        myproject

         \_ myproject-application

         \_ myproject-common

             \_ ch/myproject/common/errors/RestError.java

         \_ myproject-web

             \_ ch/myproject/web/rest/Resource.java

         \_ myproject-test

             \_ ch/myproject/test/arquillian/rest/ResourceIntTest.java

       

      myproject-application will build an ear with the war of myproject-web inside it. And myproject-web has a reference to RestError of myproject-common.

      For the testing I generate a test.jar that I add to the ear file and then deploy the ear file. So far so good.

       

      But now I want to test if the response contains the expected error when I request an inexisting resource on my REST API.

      However, if I try to parse the response body to my class RestError I'll get a NoClassDefFoundError.

       

      As I have understood, this means that the class is found during build time but later it is not found anymore.

       

      What do I do wrong?

       

       

      log:

       

          [junit] Testcase: testGetInexistingResource took 0.793 sec

          [junit] Caused an ERROR

          [junit] ch/myproject/common/errors/RestError

          [junit] java.lang.NoClassDefFoundError: ch/myproject/common/errors/RestError

          [junit] at ch.myproject.test.arquillian.rest.ResourceIntTest.testGetInexistingResource(ResourceIntTest.java:137)

          [junit] at org.jboss.arquillian.junit.Arquillian$6$1.invoke(Arquillian.java:270)

          [junit] at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60)

          [junit] at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

          [junit] at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)

          [junit] at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

          [junit] at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)

          [junit] at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)

          [junit] at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)

          [junit] at org.jboss.arquillian.container.test.impl.execution.ContainerTestExecuter.execute(ContainerTestExecuter.java:38)

          [junit] at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

          [junit] at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)

          [junit] at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

          [junit] at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:89)

          [junit] at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

          [junit] at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

          [junit] at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:75)

          [junit] at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

          [junit] at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

          [junit] at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60)

          [junit] at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

          [junit] at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

          [junit] at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)

          [junit] at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:111)

          [junit] at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:263)

          [junit] at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:226)

          [junit] at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314)

          [junit] at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)

          [junit] at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:240)

          [junit] at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:185)

          [junit] at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314)

          [junit] at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)

          [junit] at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:199)

          [junit] at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147)

          [junit] at org.jboss.arquillian.junit.container.JUnitTestRunner.execute(JUnitTestRunner.java:65)

          [junit] at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:160)

          [junit] at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:126)

          [junit] at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:90)

          [junit] at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)

          [junit] at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)

          [junit] at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)

          [junit] at ch.adnovum.jcan.ee6.web.filters.CacheHeaderFilter.doFilter(CacheHeaderFilter.java:136)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)

          [junit] at ch.adnovum.jcan.ee6.web.filters.RequestResponseLoggingFilter.doFilter(RequestResponseLoggingFilter.java:174)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)

          [junit] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)

          [junit] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)

          [junit] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)

          [junit] at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)

          [junit] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)

          [junit] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)

          [junit] at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)

          [junit] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)

          [junit] at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)

          [junit] at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)

          [junit] at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)

          [junit] at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)

          [junit] at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)

          [junit] at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)

          [junit] at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)

          [junit] at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)

          [junit] at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)

          [junit] at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)

          [junit] at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)

          [junit] at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)

          [junit] at com.sun.grizzly.ContextTask.run(ContextTask.java:71)

          [junit] at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)

          [junit] at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)

          [junit] at java.lang.Thread.run(Thread.java:722)

          [junit] Caused by: java.lang.ClassNotFoundException: ch.myproject.common.errors.RestError

          [junit] at com.sun.enterprise.loader.ASURLClassLoader.findClassData(ASURLClassLoader.java:787)

          [junit] at com.sun.enterprise.loader.ASURLClassLoader.findClass(ASURLClassLoader.java:696)

          [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:423)

          [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:356)

       

       

      Cheers,

      metalhamster

        • 1. Re: How can I solve NoClassDefFoundError
          bmajsak

          Hey,

           

          would it be possible to share deployment method? I understand that you bundle your RestError and all its required classes it the test jar, is that correct?

          • 2. Re: How can I solve NoClassDefFoundError
            metalhamster

            No, the RestError, etc. is within the myproject-common.jar which is a library of myproject-web.war.

            And both the test jar and myproject-web.war are deployed together inside an ear.

            • 3. Re: How can I solve NoClassDefFoundError
              bmajsak

              My guess would be that the test JAR you bundle in EAR does not have a reference to the common-jar. Have you tried including it in the MANIFEST file?

              • Class-Path: myproject-common.jar
              • 4. Re: How can I solve NoClassDefFoundError
                metalhamster

                I managed to solve the problem. The solution was to change the deployed ear from:

                 

                myproject-application.ear

                \_ myproject-test.jar

                \_ myproject-web.war

                      \_ WEB-INF/lib/myproject-common.jar

                           \_ RestError.class

                 

                 

                to:

                 

                 

                myproject-application.ear

                \_ myproject-web.war

                      \_ WEB-INF/lib/myproject-test.jar

                      \_ WEB-INF/lib/myproject-common.jar

                           \_ RestError.class

                 

                 

                 

                Cheers,

                metalhamster