Skrevet av Emne: Java code compiled with JDK8/JRE8 crashes when executed in JRE7  (Lest 8529 ganger)

Utlogget Floyd-ATC

  • Livstidsdiktator
  • Administrator
  • Guru
  • *****
  • Innlegg: 542
  • Karma: +12/-0
    • MSN Messenger - floyd@atc.no
    • Vis profil
    • floyd.atc.no
    • E-post
One example of code that ran perfectly well in JRE7 when compiled using JRE7:

Kode: [Velg]
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
Kode: [Velg]
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).
« Siste redigering: 08. April 2015, 22:31 pm av Floyd-ATC »


-Floyd.

--
Det finnes 10 typer mennesker;
de som forstår binærtall, de som ikke gjør det, og de som forstår Grey code.

Utlogget Cybersyn

  • n00b
  • *
  • Innlegg: 2
  • Karma: +1/-0
    • Vis profil
    • localhost
  • [applaud]0
  • [smite]0
  • If you install JRE 7 alongside JRE 8, you'll probably want to inspect the PATH environment variable to make sure JRE 7 doesn't steal the spotlight.