2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007 - INRIA - Allan CORNET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 /*--------------------------------------------------------------------------*/
15 * Loading classes at runtime.
16 * @author Allan CORNET - INRIA 2007
18 /*--------------------------------------------------------------------------*/
20 /*--------------------------------------------------------------------------*/
21 import java.lang.reflect.Method;
22 import java.lang.reflect.InvocationTargetException;
24 import java.io.IOException;
26 import java.net.URLClassLoader;
28 /*--------------------------------------------------------------------------*/
30 * ClassPath to overload java classpath.
32 public class ClassPath {
34 private static final Class[] parameters = new Class[]{URL.class};
39 protected ClassPath() {
40 /* indicate that the requested operation is not supported */
41 throw new UnsupportedOperationException();
45 * add a filename to java classpath.
47 * @throws IOException if an error occurs
49 public static void addFile(final String s) throws IOException {
53 /*--------------------------------------------------------------------------*/
55 * add a file to java classpath.
57 * @throws IOException if an error occurs
59 public static void addFile(final File f) throws IOException {
64 /*--------------------------------------------------------------------------*/
66 * add a URL to classpath.
68 * @throws IOException if an error occurs
70 public static void addURL(final URL u) throws IOException {
72 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
73 Class sysclass = URLClassLoader.class;
76 Method method = sysclass.getDeclaredMethod("addURL", parameters);
77 method.setAccessible(true);
78 method.invoke(sysloader , new Object[] {u });
79 } catch (NoSuchMethodException e) {
80 throw new IOException("Error NoSuchMethodException, could not add URL to system classloader");
81 } catch (IllegalAccessException e) {
82 throw new IOException("Error IllegalAccessException, could not add URL to system classloader");
83 } catch (InvocationTargetException e) {
84 throw new IOException("Error InvocationTargetException, could not add URL to system classloader");
88 /*--------------------------------------------------------------------------*/
90 * get the scilab classpath.
93 public static String[] getClassPath() {
95 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
96 URL[] path = sysloader.getURLs();
97 String[] paths = new String[path.length];
98 for (int i = 0; i < path.length; i++) {
99 paths[i] = path[i].getFile();
103 /*--------------------------------------------------------------------------*/
105 /*--------------------------------------------------------------------------*/