1 /*-----------------------------------------------------------------------------------*/
\r
3 * Loading classes at runtime.
\r
4 * @author Allan CORNET - INRIA 2007
\r
6 /*-----------------------------------------------------------------------------------*/
\r
7 package org.scilab.modules.jvm;
\r
8 /*-----------------------------------------------------------------------------------*/
\r
9 import java.lang.reflect.Method;
\r
10 import java.lang.reflect.InvocationTargetException;
\r
11 import java.io.File;
\r
12 import java.io.IOException;
\r
13 import java.net.URL;
\r
14 import java.net.URLClassLoader;
\r
15 import java.net.URI;
\r
16 /*--------------------------------------------------------------------------*/
\r
18 * ClassPath to overload java classpath.
\r
20 public class ClassPath {
\r
22 private static final Class[] parameters = new Class[]{URL.class};
\r
27 protected ClassPath() {
\r
28 /* indicate that the requested operation is not supported */
\r
29 throw new UnsupportedOperationException();
\r
33 * add a filename to java classpath.
\r
34 * @param s a filename
\r
35 * @throws IOException if an error occurs
\r
37 public static void addFile(final String s) throws IOException {
\r
38 File f = new File(s);
\r
41 /*--------------------------------------------------------------------------*/
\r
43 * add a file to java classpath.
\r
45 * @throws IOException if an error occurs
\r
47 public static void addFile(final File f) throws IOException {
\r
49 URI uri = f.toURI();
\r
50 addURL(uri.toURL());
\r
52 /*--------------------------------------------------------------------------*/
\r
54 * add a URL to classpath.
\r
56 * @throws IOException if an error occurs
\r
58 public static void addURL(final URL u) throws IOException {
\r
60 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
\r
61 Class sysclass = URLClassLoader.class;
\r
64 Method method = sysclass.getDeclaredMethod("addURL", parameters);
\r
65 method.setAccessible(true);
\r
66 method.invoke(sysloader , new Object[] {u });
\r
67 } catch (NoSuchMethodException e) {
\r
68 throw new IOException("Error NoSuchMethodException, could not add URL to system classloader");
\r
69 } catch (IllegalAccessException e) {
\r
70 throw new IOException("Error IllegalAccessException, could not add URL to system classloader");
\r
71 } catch (InvocationTargetException e) {
\r
72 throw new IOException("Error InvocationTargetException, could not add URL to system classloader");
\r
76 /*--------------------------------------------------------------------------*/
\r
78 * get the scilab classpath.
\r
81 public static String[] getClassPath() {
\r
83 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
\r
84 URL[] path = sysloader.getURLs();
\r
85 String[] paths = new String[path.length];
\r
86 for (int i = 0; i < path.length; i++) {
\r
87 paths[i] = path[i].getFile();
\r
91 /*--------------------------------------------------------------------------*/
\r
93 /*--------------------------------------------------------------------------*/
\r