source: src/main/java/weka/core/ClassloaderUtil.java @ 6

Last change on this file since 6 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 2.9 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    ClassloaderUtil.java
19 *    Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.core;
24
25import java.io.File;
26import java.io.IOException;
27import java.lang.reflect.Method;
28import java.net.URL;
29import java.net.URLClassLoader;
30
31/**
32 * Utility class that can add jar files to the classpath dynamically.
33 *
34 * @author Mark Hall (mhall{[at]}pentaho{[dot]}org
35 * @version  $Revision: 5953 $
36 */
37public class ClassloaderUtil
38  implements RevisionHandler {
39
40  // Parameters
41  private static final Class[] parameters = new Class[]{URL.class};
42
43  /**
44   * Add file to CLASSPATH
45   * @param s File name
46   * @throws IOException if something goes wrong when adding a file
47   */
48  public static void addFile(String s) throws IOException {
49    File f = new File(s);
50    addFile(f);
51  }
52
53  /**
54   * Add file to CLASSPATH
55   * @param f  File object
56   * @throws IOException if something goes wrong when adding a file
57   */
58  public static void addFile(File f) throws IOException {
59    addURL(f.toURL());
60  }
61
62  /**
63   * Add URL to CLASSPATH
64   * @param u URL
65   * @throws IOException if something goes wrong when adding a url
66   */
67  public static void addURL(URL u) throws IOException {
68    ClassloaderUtil clu = new ClassloaderUtil();
69    //        URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
70    URLClassLoader sysLoader = (URLClassLoader) clu.getClass().getClassLoader();
71    URL urls[] = sysLoader.getURLs();
72    for (int i = 0; i < urls.length; i++) {
73      if (urls[i].toString().toLowerCase().equals(u.toString().toLowerCase())) {
74        System.err.println("URL " + u + " is already in the CLASSPATH");
75        return;
76      }
77    }
78    Class<?> sysclass = URLClassLoader.class;
79    try {
80      Method method = sysclass.getDeclaredMethod("addURL", parameters);
81      method.setAccessible(true);
82      method.invoke(sysLoader, new Object[]{u});
83    } catch (Throwable t) {
84      t.printStackTrace();
85      throw new IOException("Error, could not add URL to system classloader");
86    }
87  }
88 
89  /**
90   * Returns the revision string.
91   *
92   * @return            the revision
93   */
94  public String getRevision() {
95    return RevisionUtils.extract("$Revision: 5953 $");
96  }
97}
Note: See TracBrowser for help on using the repository browser.