Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a NoSuchMethodError on JodaTime.withYear()?

I have a maven web app project, where I use JodaTime. JodaTime is not directly referenced in my maven project, but is a part of a transitive dependency. In other words, my web app war, has another project of mine as a direct dependency, and that jar contains JodaTime.

I am getting an error after executing these two lines. It compiles fine though.

DateTime firstDate = new DateTime();
firstDate = firstDate.withYear(2016);

And here is my error:

java.lang.NoSuchMethodError: org.joda.time.DateTime.withYear(I)Lorg/joda/time/DateTime;

I know that these kinds of errors can happen if I compile and run with different versions of a library, like this answer says, but the withYear() has been around since JodaTime 1.3, since 2006, and I can't see that I could ever have imported a version that old. I've even checked my final war-file, and the only JodaTime library present, is 2.9.2.

The two lines runs fine if I create a main-method snippet, and run it from within the same project in eclipse. They only fail upon compilation into a war file, and running from my weblogic 10.3.2 server.

Does anyone have any idea on how I can proceed to debug this one?

like image 686
jumps4fun Avatar asked Dec 18 '22 18:12

jumps4fun


1 Answers

WebLogic 10.3.6 includes this on the classpath:

joda.time_1.2.1.0.jar

This is earlier than the 1.3 that has the missing method.

Your code compiles, which is a good indication that your app's classpath has at least Joda 1.3.

Thus I suspect this is a WebLogic classpath issue. When your app uses libraries that are also on the WebLogic classpath, you need to tell WebLogic which library to use. You do this with the prefer-application-packages element in src/main/webapp/WEB-INF/weblogic.xml.

<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd">

  <context-root>myApp</context-root>
  <container-descriptor>
    <prefer-application-packages>
      <package-name>org.joda.time.*</package-name>
      <package-name>org.slf4j.*</package-name>
      <package-name>org.slf4j.impl.*</package-name>
      <package-name>org.slf4j.spi.*</package-name>
      <!-- others here -->
    </prefer-application-packages>
  </container-descriptor>

  <!-- rest of weblogic.xml here -->
</weblogic-web-app>

WebLogic has a classpath analysis tool called wls-cat to help locate these conflicts, described in this blog post. One caveat - do not just copy wls-cat's prefer-application-packages block into your webapp and think you're done - you need to resolve each conflict one by one. Sometimes that means excluding dependencies from your webapp or using scope provided.

like image 169
user944849 Avatar answered Mar 02 '23 19:03

user944849