Skip to content

Commit c109c32

Browse files
committed
ScalaScriptLanguage: clean up classpath extraction
This adds some defensive logic in case the system class loader is not a URLClassLoader (which in some environments, I think it might not be).
1 parent fd1439f commit c109c32

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/main/java/org/scijava/plugins/scripting/scala/ScalaScriptLanguage.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
import javax.script.ScriptEngine;
3838

39+
import org.scijava.log.LogService;
40+
import org.scijava.plugin.Parameter;
3941
import org.scijava.plugin.Plugin;
4042
import org.scijava.script.AdaptedScriptLanguage;
4143
import org.scijava.script.ScriptLanguage;
@@ -56,6 +58,9 @@
5658
@Plugin(type = ScriptLanguage.class, name = "Scala")
5759
public class ScalaScriptLanguage extends AdaptedScriptLanguage {
5860

61+
@Parameter
62+
private LogService log;
63+
5964
public ScalaScriptLanguage() {
6065
super("scala");
6166
}
@@ -69,13 +74,16 @@ public ScriptEngine getScriptEngine() {
6974
new NewLinePrintWriter(new ConsoleWriter(), true));
7075
}
7176

72-
/**
73-
* Uses ClassLoader to generate a string of the current classpath separated by
74-
* OS specific separator.
75-
*/
76-
private static String getClasspath() {
77-
return Arrays.stream(((URLClassLoader) ClassLoader.getSystemClassLoader())
78-
.getURLs()).map(url -> url.getPath()).collect(Collectors.joining(System
79-
.getProperty("path.separator")));
77+
/** Retrieves the current classpath as a string. */
78+
private String getClasspath() {
79+
final ClassLoader cl = ClassLoader.getSystemClassLoader();
80+
if (!(cl instanceof URLClassLoader)) {
81+
log.warn("Cannot retrieve classpath from class loader of type '" +
82+
cl.getClass().getName() + "'");
83+
return System.getProperty("java.class.path");
84+
}
85+
return Arrays.stream(((URLClassLoader) cl).getURLs()).map(//
86+
url -> url.getPath() //
87+
).collect(Collectors.joining(System.getProperty("path.separator")));
8088
}
8189
}

0 commit comments

Comments
 (0)