如何修复 Time4J 库中的 NoSuchMethodError?

Posted

技术标签:

【中文标题】如何修复 Time4J 库中的 NoSuchMethodError?【英文标题】:How can I fix the NoSuchMethodError from the Time4J Library? 【发布时间】:2020-03-08 06:11:22 【问题描述】:

我在这行代码中遇到了 NoSuchMethodError

    private CalendarPicker<PersianCalendar> calendarPicker = CalendarPicker.persianWithSystemDefaults();

当我在 intellij idea 中运行项目时它工作正常,但输出 jar 文件不会运行。 我在eclipse中导出了项目,在eclipse中出现了这个错误。

我使用了这个库:

1 - time4j-core-4.38

2 - time4j-ui-5.5

3 - time4j-calendar-4.38.jar

4 - time4j-base-5.5.jar

错误全文:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.util.ServiceConfigurationError: net.time4j.scale.LeapSecondProvider: Provider net.time4j.scale.spi.DefaultLeapSecondProviderSPI could not be instantiated
    at java.util.ServiceLoader.fail(Unknown Source)
    at java.util.ServiceLoader.access$100(Unknown Source)
    at java.util.ServiceLoader$LazyIterator.nextService(Unknown Source)
    at java.util.ServiceLoader$LazyIterator.next(Unknown Source)
    at java.util.ServiceLoader$1.next(Unknown Source)
    at net.time4j.base.ResourceLoader$StdResourceLoader.services(ResourceLoader.java:463)
    at net.time4j.scale.LeapSeconds.<init>(LeapSeconds.java:201)
    at net.time4j.scale.LeapSeconds.<clinit>(LeapSeconds.java:179)
    at net.time4j.SystemClock.calibrate(SystemClock.java:474)
    at net.time4j.SystemClock.<clinit>(SystemClock.java:95)
    at net.time4j.ui.javafx.CalendarPicker.lambda$persianWithSystemDefaults$15(CalendarPicker.java:761)
    at net.time4j.ui.javafx.CalendarControl.<init>(CalendarControl.java:82)
    at net.time4j.ui.javafx.CalendarPicker.<init>(CalendarPicker.java:178)
    at net.time4j.ui.javafx.CalendarPicker.create(CalendarPicker.java:1110)
    at net.time4j.ui.javafx.CalendarPicker.persian(CalendarPicker.java:785)
    at net.time4j.ui.javafx.CalendarPicker.persianWithSystemDefaults(CalendarPicker.java:759)
    at HomeScreenController.<init>(HomeScreenController.java:71)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at Main.start(Main.java:39)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.NoSuchMethodError: net.time4j.PlainDate.localFormatter(Ljava/lang/String;Lnet/time4j/format/ChronoPattern;)Lnet/time4j/format/TemporalFormatter;
    at net.time4j.scale.spi.DefaultLeapSecondProviderSPI.<init>(DefaultLeapSecondProviderSPI.java:77)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    ... 44 more
Exception running application Main

【问题讨论】:

【参考方案1】:

NoSuchMethodError 通常意味着您的编译时和运行时环境之间存在依赖差异。

实际上,您很可能有: 1. 类路径中net.time4j JAR 的多个不同版本 2. 运行时类路径中的net.time4j JAR 与编译时使用的不同。

由于调用层次结构似乎表明 net.time4j 本身正在调用它不知道的方法,我怀疑类路径上有多个 JAR 版本。

这似乎与您正在谈论使用的 JAR 一致。您正在混合版本4.38 和版本5.5。您应该坚持使用套件中所有库的一个版本,因为它们很可能被设计为协同工作。

您可能有点困惑,因为 Time4J 似乎更改了 4.x 和 5.x 版本之间的封装,而 time4j-core 现在可能被命名为 time4j-base

tutorial page on the Time4J website 似乎表明您可以简单地删除对 time4-coretime4j-calendar 的现有依赖项。

【讨论】:

是的,可以删除旧的 v4.38 依赖项以支持 time4j-base-5.5.jar(以及用于 JavaFX 组件的 time4j-ui-5.5)。一个小的修正:新的基本模块(在 v5.x 中)正在替换旧的核心模块和旧的日历模块(在 v4.38 中)。【参考方案2】:

由于在网上找到的不同来源,我遇到了同样的问题,混合了不同的 time4j - 版本。

所以这是不工作 - POM.XML - 导致问题的配置:

<!-- https://mvnrepository.com/artifact/net.time4j/time4j-core -->
       <dependency>
          <groupId>net.time4j</groupId>
          <artifactId>time4j-parent</artifactId>
          <version>5.5</version>
          <type>pom</type>
      </dependency>
        <dependency>
           <groupId>net.time4j</groupId>
           <artifactId>time4j-core</artifactId>
           <version>4.38</version>
       </dependency>      
<!-- https://mvnrepository.com/artifact/net.time4j/time4j-base -->
       <dependency>
           <groupId>net.time4j</groupId>
           <artifactId>time4j-base</artifactId>
           <version>5.5</version>
       </dependency>
       <dependency>
           <groupId>net.time4j</groupId>
           <artifactId>time4j-i18n</artifactId>
           <version>4.38</version>
       </dependency>
    </dependencies>

解决方案:更新 MAVEN - 存储库。 Maven 仍在使用 4.38,尽管我已更改为 5.5 - 需要清理(清除)存储库。

<!-- https://mvnrepository.com/artifact/net.time4j/time4j-base -->
       <dependency>
           <groupId>net.time4j</groupId>
           <artifactId>time4j-base</artifactId>
           <version>5.5</version>
       </dependency>
    </dependencies>


    <build>
      <pluginManagement>
        <plugins>
           <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-jar-plugin</artifactId>
             <configuration>
               <archive>
                 <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>fully.qualified.MainClass</mainClass>
                 </manifest>
               </archive>
             </configuration>
           </plugin>
           <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-dependency-plugin</artifactId>
              <version>3.1.1</version>
              <executions>
                 <execution>
                    <id>build-classpath</id>
                    <phase>generate-sources</phase>
                    <goals>
                       <goal>build-classpath</goal>
                       <goal>purge-local-repository</goal>
                     </goals>
                   <configuration>
                    <!-- configure the plugin here -->
                   </configuration>
                 </execution>
              </executions>
           </plugin>           
           <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
       </pluginManagement>
      <finalName>$project.artifactId</finalName>
    </build>

现在它就像一个魅力。

【讨论】:

以上是关于如何修复 Time4J 库中的 NoSuchMethodError?的主要内容,如果未能解决你的问题,请参考以下文章

提交后修复Mercurial存储库中的重命名

如何从iOS中的静态库构建胖库时从libWebRTC.a静态库中删除x86_64 architechture?

如何修复这个 jQuery SlideToggle?

如何为将来的私人项目保留作曲家库中的修改?

Python中的10个常见安全漏洞及修复方法

php Wordpress - 修复因使用iPhone拍摄的肖像照片在媒体库中横向显示的问题