Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8151366

Using JDK8 to compile code for JDK7 (-source 1.7 -target 1.7) does not work if ConcurrentHashMap is used

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_74"
      Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      javac 1.8.0_74

      A DESCRIPTION OF THE PROBLEM :
      Compiling Java 7 that uses java.util.concurrent.ConcurrentHashMap with JDK8 produces code that is not executable on JDK7. That is attempt to run such code fails with the exception:

      Exception in thread "main" java.lang.NoSuchMethodError: java.util.concurrent.ConcurrentHashMap.keySet()Ljava/util/concurrent/ConcurrentHashMap$KeySetView;
              at CHMCompilationTest.main(CHMCompilationTest.java:8)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Use JDK8 to compile attached CHMCompilationTest file using the following command:
      javac -source 1.7 -target 1.7 CHMCompilationTest.java
      NB: This will actually produce a warning:
      warning: [options] bootstrap class path not set in conjunction with -source 1.7
      1 warning
      2. Run compiled class using JDK7. I've used the following version:
      java version "1.7.0_80"
      Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Program runs without error and prints:
      []
      ACTUAL -
      Exception in thread "main" java.lang.NoSuchMethodError: java.util.concurrent.ConcurrentHashMap.keySet()Ljava/util/concurrent/ConcurrentHashMap$KeySetView;
              at CHMCompilationTest.main(CHMCompilationTest.java:8)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Set;
      import java.util.concurrent.ConcurrentHashMap;

      public class CHMCompilationTest {

        public static void main(String[] args) {
          ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
          Set<Integer> keys = map.keySet();
          System.out.println(keys);
        }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use JDK7 to compile Java 7 code.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: