Take the 2-minute tour ×

I have an ear which consists of 2 war files one containing junit classes and the other one containing actual application classes which are referenced by the junits. Now when executing the junits i get a java.lang.NoClassDefFoundError

Is it due to the junit class files are located in different ear and hence not able to access the application class files that are located in another ear ?


Whether this is due to the issue with deployment, Though i am able to run the application as well as some of the junits which are independent of application classes located in the other ear ?

share|improve this question

3 Answers 3

According to the strict JavaEE visibility semantics, classes inside a WAR should not be visible to other components of the same EAR. JBoss relaxes this a fair bit, and tries to flatten out the classloading hierarchy to make it less irritating, but the WAR restriction still stands.

The solution I use is to put only web resources into the WAR, and to put the WAR's class files into a seperate JAR inside the EAR. That way, the webapp itself can find the classes, and so can your unit test webapp.

share|improve this answer

The correct way is to move the common classes into a dedicated JAR, and bundle that at the EAR level. So you will have a structure like this:

  • business-logic-jar
  • main-web-app-war
  • test-web-app-war
  • application-ear

You can bundle the JAR as well as any other libs you depend on in your EAR, and reference them using the manifest file of your WARs. In MANIFEST.MF it looks like:

Class-Path: business-logic-1.0.jar spring-2.5.5.jar ...

You can still bundle additional JARs inside each WAR's WEB-INF/lib folder, e.g. junit inside the test-web-app-war. If you are using Maven, read the skinny war page for a general approach.

share|improve this answer
The entry in the MANIFEST.MF should start with ´Class-Path:...´ –  Turismo Apr 20 '11 at 9:38
Thanks for pointing that out! –  Pavel Apr 23 '11 at 13:48

JUnit classes don't belong in WAR or EAR files. They shouldn't be deployed.

You don't say which app server you're using, but if you use WebLogic you can put all your .class files into APP-INF/classes. They'll be visible at the EAR level then, so all WARs can see them.

share|improve this answer
The question is tagged "jboss". Also, running tests inside the container is fairly common practice, since we're not all blessed with "real" unit tests. –  skaffman Jul 6 '09 at 12:21
Missed the jboss tag; I was looking in the text. What's not "real" about JUnit tests? Those are as real as they get. They shouldn't be deployed, in my opinion. –  duffymo Jul 6 '09 at 12:26
I mean that many unit tests are not "pure" unit tests, they require some environmental context, such as to be running inside an appserver. This is common in legacy code bases. –  skaffman Jul 6 '09 at 12:46
Fair enough. Those are more likely to be "integration tests". And it's possible to mock away the dependencies in lots of cases. I still don't see a reason to package them and ship them off to your clients. There's got to be a way to exercise them without having to put them in the same EAR. –  duffymo Jul 6 '09 at 12:59

Your Answer

