From 86c7878dd6235109c37315380f448ac8253cae71 Mon Sep 17 00:00:00 2001 From: Karl Nelson Date: Wed, 27 Nov 2024 18:23:59 -0800 Subject: [PATCH 1/2] Jar support for jpype.class.path --- .../jpype/classloader/DynamicClassLoader.java | 57 +++++++++++++++++-- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/native/java/org/jpype/classloader/DynamicClassLoader.java b/native/java/org/jpype/classloader/DynamicClassLoader.java index 711850f86..daa29edae 100644 --- a/native/java/org/jpype/classloader/DynamicClassLoader.java +++ b/native/java/org/jpype/classloader/DynamicClassLoader.java @@ -1,6 +1,7 @@ package org.jpype.classloader; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -26,6 +27,8 @@ import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.logging.Level; +import java.util.logging.Logger; public class DynamicClassLoader extends URLClassLoader { @@ -35,7 +38,51 @@ public class DynamicClassLoader extends URLClassLoader public DynamicClassLoader(ClassLoader parent) { - super(new URL[0], parent); + super(launch(), parent); + } + + private static URL[] launch() + { + String cp = System.getProperty("jpype.class.path"); + if (cp == null) + return new URL[0]; + + ArrayList path = new ArrayList<>(); + int off = 0, next; + do + { + next = cp.indexOf(File.pathSeparator, off); + String element = (next == -1) + ? cp.substring(off) + : cp.substring(off, next); + if (!element.isEmpty()) + { + try + { + URL url = Paths.get(element).toUri().toURL(); + if (url != null) + path.add(url); + } catch (MalformedURLException ex) + { + System.err.println("Malformed url "+ element); + } catch (IOException ex) + { + System.err.println("Unable to open "+ element); + } + } + off = next + 1; + } while (next != -1); + + System.out.println("jpype.class.path " + cp); + System.clearProperty("jpype.class.path"); + System.setProperty("java.class.path", cp); + return path.toArray(new URL[0]); + } + + public void deferred() + { + System.getProperty("jpype.class.path"); + } // this is required to add a Java agent even if it is already in the path @@ -155,7 +202,7 @@ public URL getResource(String name) URL url = this.getParent().getResource(name); if (url != null) return url; - + // Otherwise search locally return findResource(name); } @@ -165,9 +212,9 @@ public URL findResource(String name) { // Check local first URL url = super.findResource(name); - if (url != null) - return url; - + if (url != null) + return url; + // Use one of the subs for (URLClassLoader cl : this.loaders) { From a7197fb1e975466b75079f1378cd22b84f3a929c Mon Sep 17 00:00:00 2001 From: Karl Nelson Date: Wed, 27 Nov 2024 18:30:31 -0800 Subject: [PATCH 2/2] Cleanup --- .../jpype/classloader/DynamicClassLoader.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/native/java/org/jpype/classloader/DynamicClassLoader.java b/native/java/org/jpype/classloader/DynamicClassLoader.java index daa29edae..b2c0fffe8 100644 --- a/native/java/org/jpype/classloader/DynamicClassLoader.java +++ b/native/java/org/jpype/classloader/DynamicClassLoader.java @@ -27,8 +27,6 @@ import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; -import java.util.logging.Level; -import java.util.logging.Logger; public class DynamicClassLoader extends URLClassLoader { @@ -41,6 +39,15 @@ public DynamicClassLoader(ClassLoader parent) super(launch(), parent); } + /** + * Special routine for handling non-ascii paths. + * + * If we are loaded as the system ClassLoader, then we will use + * "jpype.class.path" rather than "java.class.path" during the load process. + * We will move it into the expected place after so no one is the wiser. + * + * @return + */ private static URL[] launch() { String cp = System.getProperty("jpype.class.path"); @@ -48,13 +55,14 @@ private static URL[] launch() return new URL[0]; ArrayList path = new ArrayList<>(); - int off = 0, next; - do + int last = 0; + int next = 0; + + while (next!=-1) { - next = cp.indexOf(File.pathSeparator, off); - String element = (next == -1) - ? cp.substring(off) - : cp.substring(off, next); + // Find the parts + next = cp.indexOf(File.pathSeparator, last); + String element = (next == -1) ? cp.substring(last) : cp.substring(last, next); if (!element.isEmpty()) { try @@ -64,27 +72,18 @@ private static URL[] launch() path.add(url); } catch (MalformedURLException ex) { - System.err.println("Malformed url "+ element); - } catch (IOException ex) - { - System.err.println("Unable to open "+ element); + System.err.println("Malformed url in classpath skipped " + element); } } - off = next + 1; - } while (next != -1); + last = next + 1; + } - System.out.println("jpype.class.path " + cp); + // Replace the path System.clearProperty("jpype.class.path"); System.setProperty("java.class.path", cp); return path.toArray(new URL[0]); } - public void deferred() - { - System.getProperty("jpype.class.path"); - - } - // this is required to add a Java agent even if it is already in the path @SuppressWarnings("unused") private void appendToClassPathForInstrumentation(String path) throws Throwable