Skip to content

Commit

Permalink
Jar support for jpype.class.path
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Nov 28, 2024
1 parent 94f21a1 commit 86c7878
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions native/java/org/jpype/classloader/DynamicClassLoader.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand All @@ -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<URL> 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)

Check warning

Code scanning / CodeQL

Unreachable catch clause Warning

This catch-clause is unreachable; it is masked
by a previous catch-clause for exceptions of type 'MalformedURLException'
.
{
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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
{
Expand Down

0 comments on commit 86c7878

Please sign in to comment.