1 /*--------------------------------------------------------------------------*/
\r
3 * Loading classes at runtime.
\r
5 /*--------------------------------------------------------------------------*/
\r
7 /*--------------------------------------------------------------------------*/
\r
8 import java.lang.reflect.Method;
\r
10 import java.io.IOException;
\r
11 import java.net.URL;
\r
12 import java.net.URLClassLoader;
\r
13 import java.net.URI;
\r
14 /*--------------------------------------------------------------------------*/
\r
16 * ClassPath to overload java classpath.
\r
18 public class ClassPath {
\r
20 private static final Class[] parameters = new Class[]{URL.class};
\r
23 * add a filename to java classpath.
\r
24 * @param s a filename
\r
25 * @throws IOException if an error occurs
\r
27 public static void addFile(final String s) throws IOException {
\r
28 File f = new File(s);
\r
31 /*--------------------------------------------------------------------------*/
\r
33 * add a file to java classpath.
\r
35 * @throws IOException if an error occurs
\r
37 public static void addFile(final File f) throws IOException {
\r
39 URI uri = f.toURI();
\r
40 addURL(uri.toURL());
\r
42 /*--------------------------------------------------------------------------*/
\r
44 * add a URL to classpath.
\r
46 * @throws IOException if an error occurs
\r
48 public static void addURL(final URL u) throws IOException {
\r
50 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
\r
51 Class sysclass = URLClassLoader.class;
\r
54 Method method = sysclass.getDeclaredMethod("addURL", parameters);
\r
55 method.setAccessible(true);
\r
56 method.invoke(sysloader , new Object[] {u });
\r
57 } catch (Throwable t) {
\r
58 t.printStackTrace();
\r
59 throw new IOException("Error, could not add URL to system classloader");
\r
63 /*--------------------------------------------------------------------------*/
\r
65 * get the scilab classpath.
\r
68 public static String[] getClassPath() {
\r
70 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
\r
71 URL[] path = sysloader.getURLs();
\r
72 String[] paths = new String[path.length];
\r
73 for (int i = 0; i < path.length; i++) {
\r
74 paths[i] = path[i].getFile();
\r
78 /*--------------------------------------------------------------------------*/
\r
80 /*--------------------------------------------------------------------------*/
\r