4

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?

Community
  • 1
  • 1
jumps4fun
  • 3,994
  • 10
  • 50
  • 96
  • Could you post the result of`mvn dependency:tree`? Also, which server are you using? – Tunaki Mar 09 '16 at 12:26
  • weblogic 10.3.2 is my server, as stated in the question. `mvn dependency:tree`is not working in my command line. I'm using maven 2, so maybe it isn't in this version? I'll see what I can figure out. – jumps4fun Mar 09 '16 at 12:31
  • @Tunaki I am not immediately able to figure out how to make dependency:tree work – jumps4fun Mar 09 '16 at 12:36
  • Weblogic 10.3.2 is very old too, it certainly ships with an old Joda Time version. Try to configure it to use your JAR like explained here: http://stackoverflow.com/questions/7187581/how-to-set-order-of-jars-in-weblogic – Tome Mar 09 '16 at 14:48

1 Answers1

11

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.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • awesome. I was on a very tight deadline, so I made a workaround using another time api, but I did some additional research today, and your answer checks out. Thanks a lot. This will definitely come in handy later on! Weblogic 10.3.2 is an office standard, which is currently being phased out, so in a short while, I won't have to deal with it anymore. – jumps4fun Mar 10 '16 at 11:50
  • 1
    `<.container-descriptor>` should be changed to `` – Salman Oct 09 '17 at 04:49