From e69ef146c16f43b962e3a086efd52733543164f3 Mon Sep 17 00:00:00 2001 From: Miki Rozloznik Date: Mon, 9 Dec 2024 10:46:46 +0100 Subject: [PATCH] Fix by consultation!! --- ant_task/src/zserio/ant/ToolWrapper.java | 21 ++++------ .../src/zserio/tools/ExtensionManager.java | 42 +++++++------------ 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/ant_task/src/zserio/ant/ToolWrapper.java b/ant_task/src/zserio/ant/ToolWrapper.java index 9de359010..a6583361a 100644 --- a/ant_task/src/zserio/ant/ToolWrapper.java +++ b/ant_task/src/zserio/ant/ToolWrapper.java @@ -1,8 +1,10 @@ package zserio.ant; +import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; @@ -40,25 +42,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/src/zserio/tools/ExtensionManager.java b/compiler/core/src/zserio/tools/ExtensionManager.java index 6137576ba..b7404d750 100644 --- a/compiler/core/src/zserio/tools/ExtensionManager.java +++ b/compiler/core/src/zserio/tools/ExtensionManager.java @@ -4,13 +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; @@ -123,14 +123,16 @@ private ClassLoader getClassLoader() System.out.println("Found file: " + file); if (isFileZserioExtension(file)) { - urlArray.add(new URL("jar:file:" + file.getPath() + "!/")); + System.out.println("file.toURI() = " + file.toURI()); + System.out.println("file.toURI().toURL() = " + file.toURI().toURL()); + urlArray.add(file.toURI().toURL()); urlArray.addAll(getDependentJarsFromManifest(file)); } } } catch (MalformedURLException excpt) { - System.out.println("Exception!"); + System.out.println("Exception!" + excpt.toString()); return currentClassLoader; } @@ -144,32 +146,16 @@ private File getWorkingDirectory() { try { - final String execUrlPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); - final String decodedExecUrlPath = URLDecoder.decode(execUrlPath, "UTF-8"); - // check if decodedExecUrlPath is really URL, newer Java versions 11 or 17 returns normal path - System.out.println("decodedExecUrlPath = " + decodedExecUrlPath); - File decodedExecFile = null; - try - { - decodedExecFile = new File(new URI(decodedExecUrlPath)); - } - catch (URISyntaxException | IllegalArgumentException excpt) - { - decodedExecFile = new File(decodedExecUrlPath); - } - if (decodedExecFile.getPath().startsWith("file:")) - { - decodedExecFile = new File(decodedExecFile.getPath().replaceFirst("^file:", "")); - } - - System.out.println("decodedExecFile = " + decodedExecFile); - System.out.println("decodedExecFile.getParentFile() = " + decodedExecFile.getParentFile()); - System.out.println( - "decodedExecFile.getParentFile().exist = " + decodedExecFile.getParentFile().exists()); + final URI execUri = getClass().getProtectionDomain().getCodeSource().getLocation().toURI(); + System.out.println("execUri = " + execUri); + final Path execPath = Paths.get(execUri); + System.out.println("execPath = " + execPath); + final Path execParentPath = execPath.getParent(); + System.out.println("execParentPath = " + execParentPath); - return decodedExecFile.getParentFile(); + return execParentPath.toFile(); } - catch (SecurityException | UnsupportedEncodingException excpt) + catch (SecurityException | URISyntaxException excpt) { return null; }