One example of code that ran perfectly well in JRE7 when compiled using JRE7:
ConcurrentHashMap hash = /* contents here... */
for (Integer key : concurrenthashmap.keySet()) {
/* Do something with the key here */
}
However, when compiled with JRE8 this code would only run on JRE8, on JRE7 it would crash with
java.lang.NoSuchMethodError: java.util.concurrent.ConcurrentHashMap.keySet()Ljava/util/concurrent/ConcurrentHashMap$KeySetView;
Because Java, that's why.
There is no other way around this problem except to compile using JRE7. Here's how to do that with Eclipse:
- Download JRE7 from java.com and install it. (Don't worry, this will not affect your newer JREs.)
- In Eclipse, click "File", "Properties", "Java Compiler".
- Click "Configure Workspace Settings..." if you want to switch ALL projects to JRE7, otherwise check "Enable project specific settings".
- Select compliance level 1.7
- Near the bottom of the dialogue, click "Configure installed JREs".
- Click the "Add" button.
- Select "Standard VM" and click "Next".
- Next to "JRE home", click "Directory".
- Browse to "C:\Program Files\Java\jre7" and click "OK".
- Click "Finish".
- The list of installed JREs should now include "jre7". Click the checkbox next to it, then click "OK".
Eclipse should now compile your Java code to be compatible with JRE7 and complain if you are trying to use features that only exist in JRE8 (or newer).