diff --git a/CHANGES.md b/CHANGES.md
index d0de597854..4e7cb93fdc 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -7,6 +7,8 @@ You might be looking for:
### Version 1.25.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))
+* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).
+
### Version 1.24.0 - July 29th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.24.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.24.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).
diff --git a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java
index 343fb8d140..c0d8163b05 100644
--- a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java
+++ b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java
@@ -22,6 +22,8 @@
import java.util.List;
import java.util.Objects;
+import javax.annotation.Nullable;
+
/**
* This class loader is used to load classes of Spotless features from a search
* path of URLs.
@@ -59,7 +61,7 @@ class FeatureClassLoader extends URLClassLoader {
*/
FeatureClassLoader(URL[] urls, ClassLoader buildToolClassLoader) {
- super(urls, null);
+ super(urls, getParentClassLoader());
Objects.requireNonNull(buildToolClassLoader);
this.buildToolClassLoader = buildToolClassLoader;
}
@@ -74,4 +76,25 @@ protected Class> findClass(String name) throws ClassNotFoundException {
return super.findClass(name);
}
+ /**
+ * Making spotless Java 9+ compatible. In Java 8 (and minor) the bootstrap
+ * class loader saw every platform class. In Java 9+ it was changed so the
+ * bootstrap class loader does not see all classes anymore. This might lead
+ * to ClassNotFoundException in formatters (e.g. freshmark).
+ *
+ * @return null
on Java 8 (and minor), otherwise PlatformClassLoader
+ */
+ @Nullable
+ private static ClassLoader getParentClassLoader() {
+ double version = Double.parseDouble(System.getProperty("java.specification.version"));
+ if (version > 1.8) {
+ try {
+ return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
+ } catch (Exception e) {
+ throw ThrowingEx.asRuntime(e);
+ }
+ } else {
+ return null;
+ }
+ }
}
diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md
index b35765f3f8..4e5796b05a 100644
--- a/plugin-gradle/CHANGES.md
+++ b/plugin-gradle/CHANGES.md
@@ -2,6 +2,8 @@
### Version 3.25.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))
+* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).
+
### Version 3.24.0 - July 29th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.24.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.24.0))
* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).
diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md
index af865ec5e0..c994700eed 100644
--- a/plugin-maven/CHANGES.md
+++ b/plugin-maven/CHANGES.md
@@ -2,6 +2,8 @@
### Version 1.25.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))
+* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).
+
### Version 1.24.0 - July 29th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.24.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.24.0))
* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).