diff --git a/README.md b/README.md index c8b9c7428..03527c9d7 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,8 @@ not generate anything. Each Zserio extension should be packed in a single jar file. -All Zserio extensions which are available on the Java classpath are automatically loaded during Zserio compiler +All Zserio extensions which are available on the Java classpath or which are located in the directory +from which the Zserio jar executable has been run, are automatically loaded during Zserio compiler startup. More information how to implement a new Zserio extension can be found in the diff --git a/ant_task/src/zserio/ant/ToolWrapper.java b/ant_task/src/zserio/ant/ToolWrapper.java index 9de359010..c0a720947 100644 --- a/ant_task/src/zserio/ant/ToolWrapper.java +++ b/ant_task/src/zserio/ant/ToolWrapper.java @@ -1,5 +1,6 @@ package zserio.ant; +import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; @@ -40,25 +41,16 @@ public ToolWrapper(String className, Iterable classPath, boolean ignoreErr String [] files = p.list(); for (String f : files) { - String u = null; try { - if (f.endsWith(".jar")) - { - u = "jar:file:"+f+"!/"; - } - else - { - u = "file:"+f+"/"; - } - - System.out.println("Adding " + u + " to classpath"); - urls.add(new URL(u)); + final URL pathUrl = new File(f).toURI().toURL(); + System.out.println("Adding " + pathUrl + " to classpath"); + urls.add(pathUrl); } - catch (MalformedURLException e) + catch (MalformedURLException | RuntimeException e) { - throw new BuildException("Malformed URL: " + u); + throw new BuildException("Malformed URL from file: " + f + " (" + e + ")"); } } } diff --git a/compiler/core/build.xml b/compiler/core/build.xml index 04dddd2e4..36cdde263 100644 --- a/compiler/core/build.xml +++ b/compiler/core/build.xml @@ -81,7 +81,7 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spotbugs is - + diff --git a/compiler/core/src/zserio/tools/ExtensionManager.java b/compiler/core/src/zserio/tools/ExtensionManager.java index 0d0534366..f0d7682d0 100644 --- a/compiler/core/src/zserio/tools/ExtensionManager.java +++ b/compiler/core/src/zserio/tools/ExtensionManager.java @@ -4,11 +4,13 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; -import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; -import java.net.URLDecoder; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -105,11 +107,11 @@ public void callExtensions(Root rootNode, ExtensionParameters parameters) throws private ClassLoader getClassLoader() { final ClassLoader currentClassLoader = getClass().getClassLoader(); - final String workingDirectory = getWorkingDirectory(); + final File workingDirectory = getWorkingDirectory(); if (workingDirectory == null) return currentClassLoader; - final File[] fileList = new File(workingDirectory).listFiles(); + final File[] fileList = workingDirectory.listFiles(); if (fileList == null) return currentClassLoader; @@ -120,7 +122,7 @@ private ClassLoader getClassLoader() { if (isFileZserioExtension(file)) { - urlArray.add(new URL("file:" + file.getPath())); + urlArray.add(file.toURI().toURL()); urlArray.addAll(getDependentJarsFromManifest(file)); } } @@ -136,17 +138,17 @@ private ClassLoader getClassLoader() return urlClassLoader; } - private String getWorkingDirectory() + private File getWorkingDirectory() { try { - final String execPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); - final String decodedExecPath = URLDecoder.decode(execPath, "UTF-8"); - final String workingDirectory = new File(decodedExecPath).getParent(); + final URI execUri = getClass().getProtectionDomain().getCodeSource().getLocation().toURI(); + final Path execPath = Paths.get(execUri); + final Path execParentPath = execPath.getParent(); - return workingDirectory; + return (execParentPath == null) ? null : execParentPath.toFile(); } - catch (SecurityException | UnsupportedEncodingException excpt) + catch (SecurityException | URISyntaxException excpt) { return null; } diff --git a/test/core/build.xml b/test/core/build.xml index 778f102f2..a0ebf77b9 100644 --- a/test/core/build.xml +++ b/test/core/build.xml @@ -123,9 +123,10 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spo - - - + + + + diff --git a/test/extensions/build.xml b/test/extensions/build.xml index 4f3fbced7..dc06b31a0 100644 --- a/test/extensions/build.xml +++ b/test/extensions/build.xml @@ -138,9 +138,10 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spo - - - + + + +