Skip to content

Commit 1bd3c3a

Browse files
author
Jonathan Feinberg
committed
Dynamically modify classpath and library path
1 parent a81afbd commit 1bd3c3a

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

Diff for: src/jycessing/Runner.java

+69-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717

1818
import java.io.BufferedReader;
1919
import java.io.File;
20+
import java.io.FileFilter;
2021
import java.io.FileReader;
22+
import java.io.FilenameFilter;
2123
import java.io.IOException;
2224
import java.io.Reader;
25+
import java.lang.reflect.Field;
26+
import java.lang.reflect.Method;
27+
import java.net.URL;
28+
import java.net.URLClassLoader;
2329

2430
import org.python.core.Py;
2531
import org.python.core.PyString;
@@ -28,6 +34,8 @@
2834
import processing.core.PApplet;
2935

3036
public class Runner {
37+
static boolean VERBOSE = false;
38+
3139
private static String read(final Reader r) throws IOException {
3240
final BufferedReader reader = new BufferedReader(r);
3341
final StringBuilder sb = new StringBuilder(1024);
@@ -42,13 +50,73 @@ private static String read(final Reader r) throws IOException {
4250
}
4351
}
4452

53+
// See http://robertmaldon.blogspot.com/2007/11/dynamically-add-to-eclipse-junit.html
54+
private static void addJar(final URL url) throws Exception {
55+
final URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
56+
final Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
57+
method.setAccessible(true);
58+
method.invoke(classLoader, new Object[] { url });
59+
if (VERBOSE) {
60+
System.err.println("Added " + url + " to classpath.");
61+
}
62+
}
63+
64+
// See http://forums.sun.com/thread.jspa?threadID=707176
65+
private static void addLibraryPath(final String newPath) throws Exception {
66+
final Field field = ClassLoader.class.getDeclaredField("usr_paths");
67+
field.setAccessible(true);
68+
final String[] paths = (String[])field.get(null);
69+
for (final String path : paths) {
70+
if (newPath.equals(path)) {
71+
return;
72+
}
73+
}
74+
final String[] tmp = new String[paths.length + 1];
75+
System.arraycopy(paths, 0, tmp, 0, paths.length);
76+
tmp[paths.length] = newPath;
77+
field.set(null, tmp);
78+
if (VERBOSE) {
79+
System.err.println("Added " + newPath + " to java.library.path.");
80+
}
81+
}
82+
83+
private static void searchForExtraStuff(final File dir) throws Exception {
84+
final File[] dlls = dir.listFiles(new FilenameFilter() {
85+
public boolean accept(final File dir, final String name) {
86+
return name.matches("^.+\\.(so|dll|jnilib)$");
87+
}
88+
});
89+
if (dlls != null && dlls.length > 0) {
90+
addLibraryPath(dir.getAbsolutePath());
91+
}
92+
final File[] jars = dir.listFiles(new FilenameFilter() {
93+
public boolean accept(final File dir, final String name) {
94+
return name.matches("^.+\\.jar$");
95+
}
96+
});
97+
for (final File jar : jars) {
98+
addJar(jar.toURI().toURL());
99+
}
100+
final File[] dirs = dir.listFiles(new FileFilter() {
101+
public boolean accept(final File f) {
102+
return f.isDirectory() && f.getName().charAt(0) != '.';
103+
}
104+
});
105+
for (final File d : dirs) {
106+
searchForExtraStuff(d);
107+
}
108+
}
109+
45110
public static void main(final String[] args) throws Exception {
46111
if (args.length < 1) {
47112
System.err.println("I need the path of your Python script as an argument.");
48113
}
114+
if (Boolean.getBoolean("verbose")) {
115+
VERBOSE = true;
116+
}
49117
final String pathname = args[args.length - 1];
50118
final String text = read(new FileReader(pathname));
51-
119+
searchForExtraStuff(new File("libraries"));
52120
Py.initPython();
53121
final InteractiveConsole interp = new InteractiveConsole();
54122
final String path = new File(pathname).getCanonicalFile().getParent();

0 commit comments

Comments
 (0)