Java Class Loader load(String path, String className)

Here you can find the source of load(String path, String className)

Description

load

License

Apache License

Declaration

public static Class<?> load(String path, String className) throws Exception 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import com.google.common.collect.Maps;

public class Main {
    private static final Map<String, Class<?>> classMap = Maps.newConcurrentMap();

    public static Class<?> load(String path, String className) throws Exception {
        Class<?> clazz = classMap.get(className);
        if (clazz == null) {
            clazz = doLoad(path, className);
            if (clazz != null) {
                classMap.put(className, clazz);
            }/*from  w  w  w.ja va 2 s.c  o m*/
        }
        return clazz;
    }

    private static Class<?> doLoad(String path, String className) throws Exception {
        File[] files = new File(path).listFiles();
        URL urls[] = new URL[files.length];
        for (int i = 0; i < files.length; ++i) {
            urls[i] = files[i].toURI().toURL();
        }
        URLClassLoader urlClassLoader = new URLClassLoader(urls);
        return Class.forName(className, true, urlClassLoader);
    }
}

Related

  1. getStreamForPath(ClassLoader loader, String path)
  2. getToolsClassLoader()
  3. isBizServicesLocatorXmlLoaded(File f, ClassLoader classLoader)
  4. load(Class serviceClass, ClassLoader loader)
  5. load(String className)
  6. loadAllAutoConfigFiles(ClassLoader classloader)
  7. loadArrayClass(ClassLoader cl, String aName)
  8. loadClass(ClassLoader cl, String cName)
  9. loadClass(final File baseFolder, final String qualifiedType)