Skip to content

Commit

Permalink
Merge pull request #11936 from rmuir/jdkhell
Browse files Browse the repository at this point in the history
jar hell check should fail, if jars require higher java version
  • Loading branch information
rmuir committed Jun 30, 2015
2 parents 0b2070e + f20e23e commit 70fbc53
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/main/java/org/elasticsearch/bootstrap/JarHell.java
Expand Up @@ -37,6 +37,7 @@
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

/** Simple check for duplicate class files across the classpath */
class JarHell {
Expand All @@ -60,6 +61,27 @@ static void checkJarHell() throws Exception {
continue; // we can't fail because of sheistiness with joda-time
}
try (JarFile file = new JarFile(url.getPath())) {
Manifest manifest = file.getManifest();
if (manifest != null) {
// inspect Manifest: give a nice error if jar requires a newer java version
String systemVersion = System.getProperty("java.specification.version");
String targetVersion = manifest.getMainAttributes().getValue("X-Compile-Target-JDK");
if (targetVersion != null) {
float current = Float.POSITIVE_INFINITY;
float target = Float.NEGATIVE_INFINITY;
try {
current = Float.parseFloat(systemVersion);
target = Float.parseFloat(targetVersion);
} catch (NumberFormatException e) {
// some spec changed, time for a more complex parser
}
if (current < target) {
throw new IllegalStateException(path + " requires Java " + targetVersion
+ ", your system: " + systemVersion);
}
}
}
// inspect entries
Enumeration<JarEntry> elements = file.entries();
while (elements.hasMoreElements()) {
String entry = elements.nextElement().getName();
Expand Down

0 comments on commit 70fbc53

Please sign in to comment.