Example usage for java.net URLClassLoader URLClassLoader

List of usage examples for java.net URLClassLoader URLClassLoader

Introduction

In this page you can find the example usage for java.net URLClassLoader URLClassLoader.

Prototype

public URLClassLoader(String name, URL[] urls, ClassLoader parent) 

Source Link

Document

Constructs a new named URLClassLoader for the specified URLs.

Usage

From source file:de.micromata.tpsb.doc.renderer.RendererClassUtils.java

public static ClassLoader createClassLoaderFromPath(List<String> pathes) {
    List<URL> urls = new ArrayList<URL>();

    try {//from   www .  ja  v a  2s.  co  m
        for (String path : pathes) {
            File f = new File(path);
            if (f.exists() == false) {
                log.error("tpsbrendercp class path doesn't exist: " + path + " in " + f.getAbsolutePath());
                continue;
            }
            URL url = new File(path).toURI().toURL();
            urls.add(url);
        }
        URLClassLoader cl = new URLClassLoader(urls.toArray(new URL[] {}),
                RendererClassUtils.class.getClassLoader(), null) {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                try {
                    return super.loadClass(name);
                } catch (ClassNotFoundException ex) {
                    throw ex;
                }
            }
        };
        return cl;
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:gov.anl.cue.arcane.engine.matrix.MatrixModel.java

/**
 * Run the matrix model.//from w  w  w .j  a va2  s  . co m
 *
 * @return the double
 */
public Double runMatrixModel() {

    // Create a results holder.
    Double results = Double.NaN;

    // Attempt to run the model.
    try {

        // Setup a temporary class loader.
        URL[] urls = new URL[] { new File(Util.TEMP_DIR).toURI().toURL() };
        URLClassLoader classLoader = new URLClassLoader(urls, null, null);

        // Attempt to load the compiled file.
        @SuppressWarnings("rawtypes")
        Constructor constructor = classLoader
                .loadClass(MatrixModel.PACKAGE_NAME + "." + MatrixModel.CONCRETE_CLASS_NAME)
                .getDeclaredConstructor(double.class);
        constructor.setAccessible(true);
        Object object = constructor.newInstance(this.stepSize);

        // Call "matrixFormulation.step(steps)".
        Method method = object.getClass().getSuperclass().getMethod("step", int.class);
        method.invoke(object, this.stepCount);

        // Call matrixFormulation.calculateFitnessValue();
        method = object.getClass().getSuperclass().getMethod("calculateFitnessValue");
        results = (Double) method.invoke(object);

        // Clear the given class loader, which should not be
        // a child of another class loader.
        object = null;
        method = null;
        classLoader.close();
        ResourceBundle.clearCache(classLoader);
        classLoader = null;
        Introspector.flushCaches();
        System.runFinalization();
        System.gc();

        // Catch exceptions.
    } catch (Exception e) {

        // Return the default result.
        results = Double.NaN;

    }

    // Return the results.
    return results;

}