Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/no_agent' into no_a…
Browse files Browse the repository at this point in the history
…gent
  • Loading branch information
Thrameos committed Nov 28, 2024
2 parents c8ed839 + a7197fb commit d5c18dc
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 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 Down Expand Up @@ -35,7 +36,52 @@ public class DynamicClassLoader extends URLClassLoader

public DynamicClassLoader(ClassLoader parent)
{
super(new URL[0], 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");
if (cp == null)
return new URL[0];

ArrayList<URL> path = new ArrayList<>();
int last = 0;
int next = 0;

while (next!=-1)
{
// Find the parts
next = cp.indexOf(File.pathSeparator, last);
String element = (next == -1) ? cp.substring(last) : cp.substring(last, 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 in classpath skipped " + element);
}
}
last = next + 1;
}

// Replace the path
System.clearProperty("jpype.class.path");
System.setProperty("java.class.path", cp);
return path.toArray(new URL[0]);
}

// this is required to add a Java agent even if it is already in the path
Expand Down Expand Up @@ -155,7 +201,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 +211,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 d5c18dc

Please sign in to comment.