0

Trying to unit test a class which builds a static map within a class as follows:

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:

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? 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. 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.

2 Answers 2

1

It seems to be related to desugaring. The Map.entry() methos is nor available in Java 8 and 7, which is used by Android.

Sign up to request clarification or add additional context in comments.

2 Comments

Correct me if I am wrong, but if it is in the documentation for Java Platform SE 7 that means it is a part of Java 7?docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
That's the class Map.Entry, not the static method Map.entry(). You can see the documentation for the method in the documentation.
0

NoSuchMethod error tells you that the method doesn't exists. 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.

See the javadoc: https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#entry-K-V-

It says:

Since: 9

2 Comments

I thought this was introduced in Java 7?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.