0

I recently used eclipse (myeclipse actually) to create a new Web Project via the new->web project wizard. The default directory structure for the project looks as follows:

src
WebRoot
WebRoot/META-INF
WebRoot/META-INF/MANIFEST.MF
WebRoot/WEB-INF
WebRoot/WEB-INF/lib
WebRoot/WEB-INF/classes (not visible via the Package/Project Explorer)
WebRoot/WEB-INF/web.xml

For jpa I added:

WebRoot/META-INF/persistence.xml  

For spring I added:

WebRoot/META-INF/spring/spring-master.xml

When trying to run a very basic junit test to load the spring context and hibernate entity manager I was running into classpath problems. Initally to get around the first class path problem I encountered I added WebRoot/META-INF/spring to the junit->Run Configurations classpath when Spring couldn't find my spring configuration files.

But then I ran into another classpath error when jpa couldn't find my entity bean definition. Reading this article (dated 2007) it states that META-INF/persistence.xml should be contained within WEB-INF/classes because JPA searches for entity bean class files in the parent directory of META-INF/persistence.xml

When I copied META-INF (via Windows Explorer since eclipse is hiding the classes directory) and all its contents into WEB-INF/classes my classpath errors went away (as well as the need to monkey with the junit run configuration to remedy the previous problem of spring not knowing where my spring config file is located turns out I still need to add the spring folder to the junit classpath option).

But now I have configuration files in a folder which does not show in Eclipse's Package/Project Explorer.

What is the best way to setup Eclipse for this situation? Tell it to show the classes folder somehow? Why does Eclipse create META-INF as a sibling to WEB-INF for web projects?

Is there a way to inform JPA to look for entity classes somewhere other than the default?

Thanks for any input!

1 Answer 1

2

You should know that eclipse mixes the compiled classes and resource from src (and any other source code directory) in the <WebRoot>/WEB-INF/classes directory.

So one way to access the same file in Test and Webapp (for example META-INF/persistence.xml) is to put it in src/META-INF/persistence.xml. Then it is available in <WebRoot>/WEB-INF/classes/META-INF/persistence.xml for the web app, and in <classpath>/META-INF/persistence.xml for EVERY application or test.

So my recommendation is to put all the stuff that is addressed via classpath (persistence.xml, spring.xml) in a source directory, and let eclipse do the rest. - If you specify the location of a resource via classpath, then it should work the same way for webapp, test, app.

BTW: eclipse can create many source code directories, you can use one for the normal application classes, one for the class path relevant resource and one for the test classes.

2
  • Thanks Ralph! I've typically just included my test classes in src with the application classes, just in a test package. Question: Is there a reason eclipse's default Web Project structure defines a META-INF outside of a source code directory?
    – new Thrall
    Apr 28, 2011 at 15:23
  • @new Thrall: I think you are talking about WebContent\META-INF - it is the META-INF directory for the WAR (containing signatures, MANIFEST.MF,...)
    – Ralph
    Apr 29, 2011 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.