简体   繁体   English

如何解决 java.lang.NoSuchMethodError: java.util.Map.entry(Ljava/lang/Object/Ljava$Entry/Object)

[英]How to troubleshoot java.lang.NoSuchMethodError: java.util.Map.entry(Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/Map$Entry

Trying to unit test a class which builds a static map within a class as follows:尝试对 class 进行单元测试,该 class 构建 static map 如下:

private static Map<String, SomeClass> MAP = new HashMap<>();
static {
    MAP = Map.ofEntries(
              Map.entry(SOME_KEY, new SomeClass()),
               .
               .
               .
              Map.entry(SOME_KEY, new SomeClass())
          );
}

When the map is being initialized during construction of the class I am getting the following error:在构建 class 期间初始化 map 时,我收到以下错误:

java.lang.NoSuchMethodError: java.util.Map.entry(Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/Map$Entry;

at package.ClassContainingMap.<clinit>(ClassContainingMap.java:36).

I have seen a similar post post on troubleshooting this error at How do I fix a NoSuchMethodError?我在How do I fix a NoSuchMethodError? and have tried cleaning and rebuilding as suggested in one place, but to no avail.并尝试按照建议在一个地方进行清洁和重建,但无济于事。 The accepted response suggests that perhaps a separate library versions might be being used to compile and run the code, but I have no idea how to figure that out as I am relatively new to Android and Java development.接受的响应表明,可能使用单独的库版本来编译和运行代码,但我不知道如何弄清楚这一点,因为我对 Android 和 Java 开发相对较新。 I should note that the application is being built using desugaring to allow use of some newer functionality and am not sure if this may be related to that in some capacity?我应该注意到,该应用程序是使用脱糖构建的,以允许使用一些更新的功能,并且不确定这是否与某些功能有关?

Thanks in advance.提前致谢。

It seems to be related to desugaring.这似乎与脱糖有关。 The Map.entry() methos is nor available in Java 8 and 7, which is used by Android. Map.entry()方法在 Java 8 和 7 中也不可用,由 Android 使用。

NoSuchMethod error tells you that the method doesn't exists. NoSuchMethod错误告诉您该方法不存在。 This usually means that you are using the wrong version of a library at runtime.这通常意味着您在运行时使用了错误版本的库。

This happens when your working environment is not aligned with the environment where your application runs.当您的工作环境与应用程序运行的环境不一致时,就会发生这种情况。 In this case, if you work with the same libraries you have at runtime, your project won't compile.在这种情况下,如果您在运行时使用相同的库,您的项目将无法编译。

Because this is a method in the JDK and it has been introduced since JDK 9, if you are using an earlier version of the jdk to run your code, it's not going to work.因为这是 JDK 中的一种方法,并且从 JDK 9 开始引入,所以如果您使用早期版本的 jdk 来运行您的代码,它就无法工作。

See the javadoc: https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#entry-KV-请参阅 javadoc: https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#entry-KV-

It says:它说:

Since: 9自:9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NoSuchMethodError: android.util.ArrayMap.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - java.lang.NoSuchMethodError: android.util.ArrayMap.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; java.lang.NoSuchMethodError:getProperties()Ljava / util / Map; - java.lang.NoSuchMethodError: getProperties()Ljava/util/Map; java.lang.NoSuchMethodError:带有签名()Ljava.util.Map $ Entry;的方法java.util.TreeMap.firstEntry; 没有找到 - java.lang.NoSuchMethodError: method java.util.TreeMap.firstEntry with signature ()Ljava.util.Map$Entry; was not found java.lang.NoSuchMethodError:edu.stanford.nlp.util.Generics.newHashMap()Ljava / util / Map; - java.lang.NoSuchMethodError: edu.stanford.nlp.util.Generics.newHashMap()Ljava/util/Map; java.lang.NoSuchMethodError: org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V - java.lang.NoSuchMethodError: org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V java.lang.NoSuchMethodError:org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava / util / Map - java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Map Spring java.lang.NoSuchMethodError:org.springframework.web.accept.PathExtensionContentNegotiationStrategy.getMediaTypes()Ljava / util / Map; - Spring java.lang.NoSuchMethodError: org.springframework.web.accept.PathExtensionContentNegotiationStrategy.getMediaTypes()Ljava/util/Map; Hibernate 5.2.7 - java.lang.NoSuchMethodError:org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava / util / Map; - Hibernate 5.2.7 - java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Map; java.lang.NoSuchMethodError:javax.ws.rs.core.Application.getProperties()Ljava / util / Map - java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map; - java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM