问题
I'm working on a project which aims to allow a user to add some film details to a database and displays the list of existing films. I have created a Dynamic Web Project in Eclipse using JPA to communicate with a remote database. All of this works fine when I run the project in the Eclipse IDE but when I export it to a WAR and deploy it on a Tomcat server the JPA functionality no longer works. The following error is thrown:
javax.servlet.ServletException: Servlet execution threw an exception
servlets.AdminMenu.doGet(AdminMenu.java:31)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: javax/persistence/Persistence
servlets.DBOperator.getFilmList(DBOperator.java:22)
servlets.AddFilm.doGet(AddFilm.java:28)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
servlets.AdminMenu.doGet(AdminMenu.java:31)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Do I need to package my persistence.xml
etc. in a certain way before exporting and deploying?
回答1:
Most JPA libraries only come native to J2EE containers such as JBOSS, Glassfish, etc... Tomcat is a Servlet Container not a J2EE container. You would need to include the JPA implementation that you are seeking to use as a dependency and deploy it with your WAR.
来源:https://stackoverflow.com/questions/28972937/jpa-functionality-in-a-deployed-war-on-tomcat