Here’s a class I came across while trying to solve a problem I had recently. Its a ‘hack’ so to speak, but it does work well. I have an app that uses the Comm API with java. The issue I had was the nusance of installing the app. You had to copy a few files to the jre and then set some classpath stuff. Not a big deal per se, but annoying, especially when I wanted a simple drag and drop solution.
This class helped provide that, by allowing me to add the comm.jar at runtime without having to make a batch file or modify the environment variables.
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
public class ClassPathHacker {
      private static final Class[] parameters = new Class[] { URL.class };
      public static void addFile(String s) throws IOException {
            File f = new File(s);
            addFile(f);
      }
      
      public static void addFile(File f) throws IOException {
            addURL(f.toURL());
      }
      public static void addURL(URL u) throws IOException {
            URLClassLoader sysloader =
                  (URLClassLoader) ClassLoader.getSystemClassLoader();
            Class sysclass = URLClassLoader.class;
            try {
                  Method method = sysclass.getDeclaredMethod("addURL", parameters);
                  method.setAccessible(true);
                  method.invoke(sysloader, new Object[] { u });
            } catch (Throwable t) {
                  t.printStackTrace();
                  throw new IOException("Error, could not add URL to system classloader");
            }
      }
}
I don’t take credit for this, I came across it at java.net on the forums.
Regards,
Dr. A>