为什么在JodaTime.withYear()上收到NoSuchMethodError? [英] Why do I get a NoSuchMethodError on JodaTime.withYear()?

查看:130
本文介绍了为什么在JodaTime.withYear()上收到NoSuchMethodError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven Web应用程序项目,在这里我使用JodaTime.在我的Maven项目中没有直接引用JodaTime,但它是传递依赖项的一部分.换句话说,我的网络应用大战中,我的另一个项目具有直接依赖关系,并且该jar中包含JodaTime.

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);

这是我的错误:

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

我知道,如果我编译并运行不同版本的库,例如此答案说,但是withYear()自JodaTime 1.3以来(自2006年起)就出现了,我看不到我曾经导入过这么老的版本.我什至已经检查了我的最终war文件,唯一存在的JodaTime库是2.9.2.

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.

如果我创建一个主方法代码段,并在eclipse中的同一项目中运行,则这两行运行良好.它们只有在编译成war文件并从我的weblogic 10.3.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?

推荐答案

WebLogic 10.3.6将其包含在类路径中:

WebLogic 10.3.6 includes this on the classpath:

joda.time_1.2.1.0.jar

这早于缺少方法的1.3.

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

您的代码已编译,这很好地表明您的应用的类路径至少具有Joda 1.3.

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

因此,我怀疑这是WebLogic类路径问题.当您的应用使用也在WebLogic类路径上的库时,您需要告诉WebLogic要使用哪个库.您可以使用src/main/webapp/WEB-INF/weblogic.xml中的prefer-application-packages元素执行此操作.

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具有名为wls-cat的类路径分析工具,可帮助找到这些冲突,如

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.

这篇关于为什么在JodaTime.withYear()上收到NoSuchMethodError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆