Java EE: NoClassDefFoundError org.joda.time.DateTime with IntelliJ and Maven

丶灬走出姿态 提交于 2019-12-13 18:29:57

问题


I am using IntelliJ 14

I want to add joda.time lib in my project. When I add this lib manually (copy the jar file into lib repository, and add reference in Project Structure) everything work good, I can use the library in a servlet and show the result in a jsp.

But in second step I create the same project but I am using maven with a pom.xml file. I add this dependency :

<dependencies>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.1</version>
    </dependency>
</dependencies>

Maven download automatically the lib, in my Project Structure every thing looks like it's good, I run the project ... no error, but when I go to my webpage I have an error in my navigator :

java.lang.NoClassDefFoundError: org/joda/time/DateTime
com.sdzee.servlets.ServletTest.doGet(ServletTest.java:47)
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)

The project is exactly the same, I just use Maven for use the lib.

Do you have ideas?

My Servlet line 47 for example :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    DateTime dt = new DateTime();
    Integer jourDuMois = dt.getDayOfMonth();
}

回答1:


You should check in your pom.xml if the packaging type is explicitly set to war. If not, maven won't use the maven-war-plugin to bundle your application correctly with all your referenced libraries, e.g. joda-time.

so You should have:

<packaging>war</packaging>

See also for reference: Maven Reference Book




回答2:


Thank MWiesner, you for your Answer. It work.

I found another solution:

  • In IntelliJ / Project Structure (F4 Key) / Artifacts
  • On the right (Available Elements) there is the joda-time library, i must double click it fo transfer the lib in the artifact.

I dont know if is it possible for IntelliJ to do that automatically when maven import the lib ?




回答3:


The problem is more likely the joda-time jar is not included when you deploy the project. You can unzip the war file and see if it's there. I use Eclipse, which installs Maven plugin. So when I export the Maven project, all the Maven managed jars will be included in the war.



来源:https://stackoverflow.com/questions/28637218/java-ee-noclassdeffounderror-org-joda-time-datetime-with-intellij-and-maven

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