Search code examples
javajava-8concurrenthashmap

"Undefined reference: .. ConcurrentHashMap.keySet()" when building in Java 8


i have a project, and i am build this project with jdk 6,7,8 and my target is 1.6

when i build jdk 8 i get this error:

Undefined reference: java.util.concurrent.ConcurrentHashMap.KeySetView java.util.concurrent.ConcurrentHashMap.keySet()

since i have this code in that line:

   final Iterator<CLASS_NAME> itr = hashMap.keySet().iterator();

how can avoid error, i made some search in internet, and since java 8 changed its return type keyset, i got error. is this any solution. i am using maven, and animal-sniffer-plugin gives this error, with signature error.


Solution

  • Another Answer suggests a modification to your code (using keys() instead of keySet()) so that you can compile your source code on Java 8 and run on Java 7. I think that is a retrograde step.

    Instead:

    • If your aim is to create a production build of your software that will run on Java 6, 7 and 8, then your best bet is to do your production builds on JDK 6.

    • If your aim is to do your development builds on Java 8 (but stay backwards compatible at the source code level for now), then change the maven plugin configs for animal-sniffer to ignore these classes; see http://mojo.codehaus.org/animal-sniffer-maven-plugin/examples/checking-signatures.html for an explanation.

      However, there is a risk that animal-sniffer will ignore too much; e.g. it won't tell you if you use new Java 8 methods in ConcurrentHashMap. You will need to allow for that possibility ....

    • If your aim is to move to Java 8 (so that you can start using new Java 8 features in your code), then just do it. Your code won't be backward compatible, but you can't support old versions of Java for ever ...

    (These suggestions are not mutually exclusive, if you consider the big picture.)