Permalink
Browse files

Dynamically modify classpath and library path

  • Loading branch information...
1 parent a81afbd commit 1bd3c3afecce4c619cd0ac4dbdf9b499dd4e26a4 @jdf committed Aug 12, 2010
Showing with 69 additions and 1 deletion.
  1. +69 −1 src/jycessing/Runner.java
View
70 src/jycessing/Runner.java
@@ -17,9 +17,15 @@
import java.io.BufferedReader;
import java.io.File;
+import java.io.FileFilter;
import java.io.FileReader;
+import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Reader;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
import org.python.core.Py;
import org.python.core.PyString;
@@ -28,6 +34,8 @@
import processing.core.PApplet;
public class Runner {
+ static boolean VERBOSE = false;
+
private static String read(final Reader r) throws IOException {
final BufferedReader reader = new BufferedReader(r);
final StringBuilder sb = new StringBuilder(1024);
@@ -42,13 +50,73 @@ private static String read(final Reader r) throws IOException {
}
}
+ // See http://robertmaldon.blogspot.com/2007/11/dynamically-add-to-eclipse-junit.html
+ private static void addJar(final URL url) throws Exception {
+ final URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
+ final Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
+ method.setAccessible(true);
+ method.invoke(classLoader, new Object[] { url });
+ if (VERBOSE) {
+ System.err.println("Added " + url + " to classpath.");
+ }
+ }
+
+ // See http://forums.sun.com/thread.jspa?threadID=707176
+ private static void addLibraryPath(final String newPath) throws Exception {
+ final Field field = ClassLoader.class.getDeclaredField("usr_paths");
+ field.setAccessible(true);
+ final String[] paths = (String[])field.get(null);
+ for (final String path : paths) {
+ if (newPath.equals(path)) {
+ return;
+ }
+ }
+ final String[] tmp = new String[paths.length + 1];
+ System.arraycopy(paths, 0, tmp, 0, paths.length);
+ tmp[paths.length] = newPath;
+ field.set(null, tmp);
+ if (VERBOSE) {
+ System.err.println("Added " + newPath + " to java.library.path.");
+ }
+ }
+
+ private static void searchForExtraStuff(final File dir) throws Exception {
+ final File[] dlls = dir.listFiles(new FilenameFilter() {
+ public boolean accept(final File dir, final String name) {
+ return name.matches("^.+\\.(so|dll|jnilib)$");
+ }
+ });
+ if (dlls != null && dlls.length > 0) {
+ addLibraryPath(dir.getAbsolutePath());
+ }
+ final File[] jars = dir.listFiles(new FilenameFilter() {
+ public boolean accept(final File dir, final String name) {
+ return name.matches("^.+\\.jar$");
+ }
+ });
+ for (final File jar : jars) {
+ addJar(jar.toURI().toURL());
+ }
+ final File[] dirs = dir.listFiles(new FileFilter() {
+ public boolean accept(final File f) {
+ return f.isDirectory() && f.getName().charAt(0) != '.';
+ }
+ });
+ for (final File d : dirs) {
+ searchForExtraStuff(d);
+ }
+ }
+
public static void main(final String[] args) throws Exception {
if (args.length < 1) {
System.err.println("I need the path of your Python script as an argument.");
}
+ if (Boolean.getBoolean("verbose")) {
+ VERBOSE = true;
+ }
final String pathname = args[args.length - 1];
final String text = read(new FileReader(pathname));
-
+ searchForExtraStuff(new File("libraries"));
Py.initPython();
final InteractiveConsole interp = new InteractiveConsole();
final String path = new File(pathname).getCanonicalFile().getParent();

0 comments on commit 1bd3c3a

Please sign in to comment.