1 /*--------------------------------------------------------------------------*/
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) INRIA - Allan CORNET
5 * Copyright (C) 2008 - INRIA - Sylvestre LEDRU
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 /*--------------------------------------------------------------------------*/
17 /*--------------------------------------------------------------------------*/
18 import java.lang.reflect.Method;
19 import java.lang.reflect.InvocationTargetException;
21 import java.io.IOException;
23 import java.net.URLClassLoader;
25 import java.util.Iterator;
26 import java.util.Vector;
27 /*--------------------------------------------------------------------------*/
29 * ClassPath to overload java classpath.
31 public class ClassPath {
33 private static final Class[] parameters = new Class[]{URL.class};
35 private static Vector<URL> queued = new Vector<URL>();
38 * add a filename to java classpath.
40 * @throws IOException if an error occurs
42 public static void addFile(final String s,int i) throws IOException {
43 addFile(new File(s), i);
45 /*-----------------------------------------------------------------------*/
47 * add a file to java classpath.
49 * @throws IOException if an error occurs
51 public static void addFile(final File f, int i) throws IOException {
52 addURL(f.toURI().toURL(), i);
55 /*-----------------------------------------------------------------------*/
57 * Add a URL to classpath.
58 * @param u URL of the classes (jar or path)
59 * @param i the type of load: i=0 startup / i=1 background / i=2 onUse
61 public static void addURL(final URL u, int i) {
63 final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
64 Class sysclass = URLClassLoader.class;
68 final Method method = sysclass.getDeclaredMethod("addURL", parameters);
69 method.setAccessible(true);
71 case 0: /* Load now */
72 method.invoke(sysloader , new Object[] { u });
74 case 1: /* Load later (background) */
79 } catch (NoSuchMethodException e) {
80 System.err.println("Error: Cannot find the declared method: " + e.getLocalizedMessage());
81 } catch (IllegalAccessException e) {
82 System.err.println("Error: Illegal access: " + e.getLocalizedMessage());
83 } catch (InvocationTargetException e) {
84 System.err.println("Error: Could not invocate target: " + e.getLocalizedMessage());
88 /*-----------------------------------------------------------------------*/
90 * Get the classpath loaded
91 * @return classpath The list of the 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();
106 * Load all the classpath in dedicated threads in background
108 public static void loadBackGroundClassPath(){
109 Thread backgroundLoader = new Thread() {
113 Iterator<URL> urlIt = queued.iterator();
115 while (urlIt.hasNext()) {
116 ClassPath.addURL(urlIt.next(),0);
119 }catch (Exception e){
120 System.err.println("Error : "+e.getLocalizedMessage());
124 backgroundLoader.start();
127 /*--------------------------------------------------------------------------*/