From 1cf4665c53b4e36cb200193a24e63788cbc36fd1 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 25 Jan 2018 19:50:58 -0800 Subject: [PATCH] Make classloader handling more compatible with JDK 9 From http://www.oracle.com/technetwork/java/javase/9-relnote-issues-3704069.html: The application class loader is no longer an instance of java.net.URLClassLoader (an implementation detail that was never specified in previous releases). Code that assumes that ClassLoader::getSytemClassLoader returns a URLClassLoader object will need to be updated. Note that Java SE and the JDK do not provide an API for applications or libraries to dynamically augment the class path at run-time. --- .../sdk/runners/DataflowPipelineRunner.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/sdk/src/main/java/com/google/cloud/dataflow/sdk/runners/DataflowPipelineRunner.java b/sdk/src/main/java/com/google/cloud/dataflow/sdk/runners/DataflowPipelineRunner.java index e7589bfef4..75d7a85336 100644 --- a/sdk/src/main/java/com/google/cloud/dataflow/sdk/runners/DataflowPipelineRunner.java +++ b/sdk/src/main/java/com/google/cloud/dataflow/sdk/runners/DataflowPipelineRunner.java @@ -136,6 +136,8 @@ import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Optional; +import com.google.common.base.Splitter; +import com.google.common.base.StandardSystemProperty; import com.google.common.base.Strings; import com.google.common.base.Utf8; import com.google.common.collect.ForwardingMap; @@ -3198,23 +3200,30 @@ public String toString() { * @return A list of absolute paths to the resources the class loader uses. */ protected static List detectClassPathResourcesToStage(ClassLoader classLoader) { - if (!(classLoader instanceof URLClassLoader)) { + List files = new ArrayList<>(); + if (classLoader == ClassLoader.getSystemClassLoader()) { + for (String element : + Splitter.on(File.pathSeparatorChar) + .split(StandardSystemProperty.JAVA_CLASS_PATH.value())) { + files.add(new File(element).getAbsolutePath()); + } + } else if (classLoader instanceof URLClassLoader) { + for (URL url : ((URLClassLoader) classLoader).getURLs()) { + try { + files.add(new File(url.toURI()).getAbsolutePath()); + } catch (IllegalArgumentException | URISyntaxException e) { + String message = String.format("Unable to convert url (%s) to file.", url); + LOG.error(message); + throw new IllegalArgumentException(message, e); + } + } + } else { String message = String.format("Unable to use ClassLoader to detect classpath elements. " - + "Current ClassLoader is %s, only URLClassLoaders are supported.", classLoader); + + "Current ClassLoader is %s, only URLClassLoaders and the system ClassLoader are " + + "supported.", classLoader); LOG.error(message); throw new IllegalArgumentException(message); } - - List files = new ArrayList<>(); - for (URL url : ((URLClassLoader) classLoader).getURLs()) { - try { - files.add(new File(url.toURI()).getAbsolutePath()); - } catch (IllegalArgumentException | URISyntaxException e) { - String message = String.format("Unable to convert url (%s) to file.", url); - LOG.error(message); - throw new IllegalArgumentException(message, e); - } - } return files; }