Example usage for java.lang ClassNotFoundException ClassNotFoundException

List of usage examples for java.lang ClassNotFoundException ClassNotFoundException

Introduction

In this page you can find the example usage for java.lang ClassNotFoundException ClassNotFoundException.

Prototype

public ClassNotFoundException(String s) 

Source Link

Document

Constructs a ClassNotFoundException with the specified detail message.

Usage

From source file:org.webical.plugin.classloading.PluginClassLoader.java

/**
 * Override of the default method. First tries the parent {@link ClassLoader}
 * (Standard practice) and then our implementation (first the jar registrations and then the class registrations)
 * @see java.lang.ClassLoader#findClass(java.lang.String)
 *///  w ww .j a v a  2s  . co  m
@Override
public Class<?> findClass(String className) throws ClassNotFoundException {
    if (StringUtils.isEmpty(className)) {
        throw new ClassNotFoundException("Cannot load a class without a name: " + className);
    }

    if (definedClassesMap.containsKey(className)) {
        return definedClassesMap.get(className);
    }

    //Try the default ClassLoader (standard practice)
    try {
        if (log.isDebugEnabled()) {
            log.debug("Trying to load class: " + className + " through the default ClassLoader");
        }
        return getParent().loadClass(className);
    } catch (ClassNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug("Default ClassLoader could not load class: " + className);
        }
    }

    byte[] classBytes = null;

    //First try the loaded jar files
    if (log.isDebugEnabled()) {
        log.debug("Trying to load class: " + className + " from the registered jar files");
    }
    classBytes = (byte[]) classArrays.get(className);
    if (classBytes != null) {
        if (log.isDebugEnabled())
            log.debug("Class found in jar file");
        Class clazz = defineClass(className, classBytes, 0, classBytes.length, null);
        if (clazz != null) {
            definedClassesMap.put(className, clazz);
        }
        return clazz;
    }

    //Not found, use our implementation
    //Locate and read in the class file
    File classFile = classNameToFileMap.get(className);
    if (classFile == null) {
        log.error("Class does not seem to be registered: " + className);
        throw new ClassNotFoundException("Class does not seem to be registered: " + className);
    }
    if (!classFile.canRead()) {
        log.error("Class does not seem to be readable: " + className + " file: " + classFile.getAbsolutePath());
        throw new ClassNotFoundException(
                "Class does not seem to be readable: " + className + " file: " + classFile.getAbsolutePath());
    }

    FileInputStream classInputStream = null;
    try {
        classInputStream = new FileInputStream(classFile);
        classBytes = getBytesFromFile(classInputStream, classFile.length());
    } catch (FileNotFoundException e) {
        log.error("Class: " + className + " could not be found at registered location: "
                + classFile.getAbsolutePath());
        throw new ClassNotFoundException("Class: " + className + " could not be found at registered location: "
                + classFile.getAbsolutePath());
    } catch (IOException e) {
        log.error("Class: " + className + " could not be read from file " + classFile.getAbsolutePath(), e);
        throw new ClassNotFoundException(
                "Class: " + className + " could not be read from file " + classFile.getAbsolutePath(), e);
    } finally {
        if (classInputStream != null) {
            try {
                classInputStream.close();
            } catch (IOException e) {
                log.error("Could not close FileInputStream for file: " + classFile.getAbsolutePath());
            }
        }
    }

    if (log.isDebugEnabled()) {
        log.debug("Successfully loaded in the bytes for class: " + className);
    }

    //Define and return the class
    try {
        Class clazz = defineClass(className.substring(className.lastIndexOf("/") + 1), classBytes, 0,
                classBytes.length);
        if (clazz != null) {
            definedClassesMap.put(className, clazz);
        }
        return clazz;
    } catch (ClassFormatError e) {
        log.error("Error parsing class file: " + classFile.getAbsolutePath(), e);
        throw new ClassNotFoundException("Error parsing class file: " + classFile.getAbsolutePath(), e);
    }
}

From source file:com.hurence.logisland.classloading.PluginLoader.java

/**
 * Load a plugin by autoproxying between current caller classloader and plugin own classloader.
 *
 * @param className the name of plugin class to load
 * @param <U>       the return type.
 * @return an instance of the requested plugin
 * @throws ClassNotFoundException//from   w  ww  .  j  a va 2  s .co  m
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
public static <U> U loadPlugin(String className)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    ClassLoader cl = registry.get(className);
    if (cl == null) {
        throw new ClassNotFoundException(
                "Unable to find component with class " + className + ". Please check your classpath");
    }
    ClassLoader thiz = Thread.currentThread().getContextClassLoader();
    Object o;
    try {
        Class<?> cls = cl.loadClass(className);
        Thread.currentThread().setContextClassLoader(cl);
        o = cls.newInstance();
    } finally {
        Thread.currentThread().setContextClassLoader(thiz);
    }
    return (U) PluginProxy.create(o);
}

From source file:org.apache.tomcat.util.net.SSLImplementation.java

public static SSLImplementation getInstance(String className) throws ClassNotFoundException {
    if (className == null)
        return getInstance();

    try {//from  w  w  w. j a  v  a  2 s .c om
        // Workaround for the J2SE 1.4.x classloading problem (under Solaris).
        // Class.forName(..) fails without creating class using new.
        // This is an ugly workaround. 
        if (JSSEImplementationClass.equals(className)) {
            return new org.apache.tomcat.util.net.jsse.JSSEImplementation();
        }
        Class clazz = Class.forName(className);
        return (SSLImplementation) clazz.newInstance();
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.debug("Error loading SSL Implementation " + className, e);
        throw new ClassNotFoundException("Error loading SSL Implementation " + className + " :" + e.toString());
    }
}

From source file:org.eclipse.ecr.runtime.osgi.OSGiRuntimeActivator.java

public Class<?> loadClass(String bundleName, String className) throws Exception {
    Bundle bundle = getBundle(bundleName);
    if (bundle == null) {
        throw new ClassNotFoundException(
                "No bundle found with name: " + bundleName + ". Unable to load class " + className);
    }//from  w ww.  j  a va  2 s  .c  o  m
    return bundle.loadClass(className);
}

From source file:ch.frankel.vaadin.spring.SpringApplicationServlet.java

/**
 * Get the application class from the bean configured in Spring's context.
 * //from   w ww.ja va2 s  .com
 * @see AbstractApplicationServlet#getApplicationClass()
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {

    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

    if (wac == null) {

        throw new ClassNotFoundException("Cannot get an handle on Spring's context. Is Spring running? "
                + "Check there's an org.springframework.web.context.ContextLoaderListener configured.");
    }

    Object bean = wac.getBean(name);

    if (bean == null) {

        throw new ClassNotFoundException("No application bean found under name " + name);
    }

    return (Class) bean.getClass();
}

From source file:com.vaadin.terminal.gwt.server.SpringApplicationOSGiServlet.java

@Override
protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {
    ApplicationContext springContext = getApplicationContext();

    if (!springContext.containsBean(beanParam)) {

        throw new ClassNotFoundException("No application bean found under name " + beanParam);
    }//from ww  w .j av a  2s.  co m

    return (Class<? extends Application>) springContext.getBean(beanParam).getClass();
}

From source file:com.ottogroup.bi.asap.repository.CachedComponentClassLoader.java

/**
 * Loads referenced class:/*from   w w w. j  a  v  a2s . c  om*/
 * <ul>
 *   <li>find in already loaded classes</li>
 *   <li>look it up in previously loaded and thus cached classes</li>
 *   <li>find it in jar files</li>
 *   <li>ask parent class loader</li>
 * </ul>
 * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
 */
public Class<?> loadClass(String name) throws ClassNotFoundException {

    synchronized (getClassLoadingLock(name)) {

        // check if class has already been loaded
        Class<?> clazz = findLoadedClass(name);
        if (clazz != null)
            return clazz;

        // check internal cache for already loaded classes
        clazz = cachedClasses.get(name);
        if (clazz != null) {
            return clazz;
        }

        // check if the managed jars contain the class
        if (byteCode.containsKey(name))
            clazz = findClass(name);
        if (clazz != null) {
            return clazz;
        }

        // otherwise hand over the request to the parent class loader
        clazz = super.loadClass(name);
        if (clazz == null)
            throw new ClassNotFoundException("Class '" + name + "' not found");

        return clazz;
    }
}

From source file:com.zotoh.maedr.etc.CmdDevice.java

private void cfgDev(String dev) throws Exception {
    JSONObject top = loadConf(getCwd());
    if (!existsDevice(top, dev)) {
        throw new Exception("Unknown device type: " + dev);
    }/*from w w w .j  a v  a 2  s.c  o m*/
    Class<?> z;
    if (DefaultDeviceFactory.getAllDefaultTypes().contains(dev)) {
        z = DefaultDeviceFactory.getDevCZ(dev);
    } else {
        z = getUserDevCZ(top, dev);
    }

    if (z == null) {
        throw new ClassNotFoundException("Class not found for device: " + dev);
    }

    Device d = (Device) z.getConstructor(DeviceManager.class).newInstance(_dummyDevMgr);
    Properties props = new Properties();
    boolean ok = false;

    if (d != null && d.supportsConfigMenu()) {
        ok = d.showConfigMenu(rcb(), props);
    }

    if (!ok) {
        return;
    }

    String id = nsb(props.remove("_id"));
    tstEStrArg("device id", id);
    props.put("type", dev);
    JSONObject obj = new JSONObject(props);
    JSONObject g = getDevs(top);

    if (g.has(id)) {
        throw new Exception("Another device with name \"" + id + "\" is defined already");
    } else {
        g.put(id, obj);
    }

    saveConf(getCwd(), top);
}

From source file:org.springframework.data.keyvalue.riak.util.RiakClassLoader.java

@Override
protected Class<?> findClass(String s) throws ClassNotFoundException {
    Class<?> c;//from w  w w  .  j  av a  2 s  .  c  o m
    try {
        c = super.findClass(s);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Found class '%s' locally defined.", s));
        }
    } catch (Throwable t) {
        // Class not defined in this ClassLoader yet
    }

    Set<String> buckets = new LinkedHashSet<String>(this.buckets);
    if (null != defaultBucket) {
        buckets.add(defaultBucket);
    }
    for (String bucket : buckets) {
        if (bucket.indexOf("/") < 0) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Class '%s' not locally defined, trying Riak.", s));
                }
                byte[] buff = riakTemplate.getAsBytes(URLEncoder.encode(bucket, "UTF-8"), s);
                c = defineClass(s, buff, 0, buff.length);
                if (null != c) {
                    return c;
                }
            } catch (ClassFormatError ignored) {
            } catch (UnsupportedEncodingException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    // Nothing found
    throw new ClassNotFoundException("Class not found: " + s);
}

From source file:org.ms123.common.libhelper.FileSystemClassLoader.java

public synchronized Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
    //System.out.println("\tloadClass:"+className);
    Class<?> toReturn = null;
    if (!isValid) {
        //System.out.println("loadClass:isValid:"+this);
        return toReturn;
    }// w w  w.  j a  v  a  2 s .  co m
    if (toReturn == null) {
        toReturn = getAlreadyLoadedClass(className);
    }
    if (loadCustomClassFirst) {
        if (toReturn == null) {
            toReturn = loadCustomClass(className);
        }
        if (toReturn == null) {
            toReturn = loadParentClass(className);
        }
    } else {
        if (toReturn == null) {
            toReturn = loadParentClass(className);
        }
        if (toReturn == null) {
            toReturn = loadCustomClass(className);
        }
    }
    if (toReturn == null) {
        debug("Class not found: " + className);
        throw new ClassNotFoundException(className);
    } else {
        if (resolve) {
            resolveClass(toReturn);
        }
    }
    return toReturn;
}