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:plugspud.PluginManager.java

/**
 * Load a class and instantiate that is accessable by the specified plugins
 * class loader./*ww w. j  a va 2s.  c o m*/
 * 
 * @param plugin
 *            plugin name
 * @param className
 *            class name to load
 * @return class
 * @throws ClassNotFoundException
 *             if class cannot be loaded
 */
public Class loadClass(String plugin, String className) throws ClassNotFoundException {
    Plugin p = getPlugin(plugin);
    if (p == null) {
        throw new ClassNotFoundException("The plugin " + plugin + " could not be located.");
    }
    return p.getClass().getClassLoader().loadClass(className);
}

From source file:ar.com.fluxit.jqa.bce.bcel.BCERepositoryImpl.java

@Override
public Type lookupType(Class<?> clazz) throws ClassNotFoundException {
    // TODO cache
    final org.apache.bcel.classfile.JavaClass lookupClass = org.apache.bcel.Repository.lookupClass(clazz);
    if (lookupClass == null) {
        throw new ClassNotFoundException("Class not found: " + clazz.getName());
    } else {/*from   ww  w.  j a va2s .  c  om*/
        return BcelJavaType.create(lookupClass);
    }
}

From source file:dalma.container.ClassLoaderImpl.java

/**
 * Finds a class on the given classpath.
 *
 * @param name The name of the class to be loaded. Must not be
 *             <code>null</code>.
 *
 * @return the required Class object//from w  w  w  .  j a  v a 2s . c o m
 *
 * @exception ClassNotFoundException if the requested class does not exist
 * on this loader's classpath.
 */
private Class findClassInComponents(String name) throws ClassNotFoundException {
    // we need to search the components of the path to see if
    // we can find the class we want.
    InputStream stream = null;
    String classFilename = getClassFilename(name);
    try {
        Enumeration e = pathComponents.elements();
        while (e.hasMoreElements()) {
            File pathComponent = (File) e.nextElement();
            try {
                stream = getResourceStream(pathComponent, classFilename);
                if (stream != null) {
                    logger.finer("Loaded from " + pathComponent + " " + classFilename);
                    return getClassFromStream(stream, name, pathComponent);
                }
            } catch (SecurityException se) {
                throw se;
            } catch (IOException ioe) {
                // ioe.printStackTrace();
                logger.fine(
                        "Exception reading component " + pathComponent + " (reason: " + ioe.getMessage() + ")");
            }
        }

        throw new ClassNotFoundException(name);
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:org.atricore.idbus.kernel.main.databinding.JAXBUtils.java

private static ArrayList<Class> getClassesFromDirectory(String pkg, ClassLoader cl)
        throws ClassNotFoundException {
    // This will hold a list of directories matching the pckgname. There may be more than one if a package is split over multiple jars/paths
    String pckgname = pkg;//from  w  w  w .ja  va2 s.co  m
    ArrayList<File> directories = new ArrayList<File>();
    try {
        String path = pckgname.replace('.', '/');
        // Ask for all resources for the path
        Enumeration<URL> resources = cl.getResources(path);
        while (resources.hasMoreElements()) {
            directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(), "UTF-8")));
        }
    } catch (UnsupportedEncodingException e) {
        if (log.isDebugEnabled()) {
            log.debug(pckgname + " does not appear to be a valid package (Unsupported encoding)");
        }
        throw new ClassNotFoundException(
                pckgname + " might not be a valid package because the encoding is unsupported.");
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug("IOException was thrown when trying to get all resources for " + pckgname);
        }
        throw new ClassNotFoundException(
                "An IOException error was thrown when trying to get all of the resources for " + pckgname);
    }

    ArrayList<Class> classes = new ArrayList<Class>();
    // For every directory identified capture all the .class files
    for (File directory : directories) {
        if (log.isDebugEnabled()) {
            log.debug("  Adding JAXB classes from directory: " + directory.getName());
        }
        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (String file : files) {
                // we are only interested in .class files
                if (file.endsWith(".class")) {
                    // removes the .class extension
                    // TODO Java2 Sec
                    String className = pckgname + '.' + file.substring(0, file.length() - 6);
                    try {
                        Class clazz = forName(className, false, getContextClassLoader());
                        // Don't add any interfaces or JAXWS specific classes.
                        // Only classes that represent data and can be marshalled
                        // by JAXB should be added.
                        if (!clazz.isInterface()
                                && (clazz.isEnum() || getAnnotation(clazz, XmlType.class) != null
                                        || ClassUtils.getDefaultPublicConstructor(clazz) != null)
                                && !ClassUtils.isJAXWSClass(clazz) && !isSkipClass(clazz)
                                && !Exception.class.isAssignableFrom(clazz)) {

                            // Ensure that all the referenced classes are loadable too
                            clazz.getDeclaredMethods();
                            clazz.getDeclaredFields();

                            if (log.isDebugEnabled()) {
                                log.debug("Adding class: " + file);
                            }
                            classes.add(clazz);

                            // REVIEW:
                            // Support of RPC list (and possibly other scenarios) requires that the array classes should also be present.
                            // This is a hack until we can determine how to get this information.

                            // The arrayName and loadable name are different.  Get the loadable
                            // name, load the array class, and add it to our list
                            //className += "[]";
                            //String loadableName = ClassUtils.getLoadableClassName(className);

                            //Class aClazz = Class.forName(loadableName, false, Thread.currentThread().getContextClassLoader());
                        }
                        //Catch Throwable as ClassLoader can throw an NoClassDefFoundError that
                        //does not extend Exception
                    } catch (Throwable e) {
                        if (log.isDebugEnabled()) {
                            log.debug("Tried to load class " + className
                                    + " while constructing a JAXBContext.  This class will be skipped.  Processing Continues.");
                            log.debug("  The reason that class could not be loaded:" + e.toString());
                            log.trace(JavaUtils.stackToString(e));
                        }
                    }

                }
            }
        }
    }

    return classes;
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

private static ArrayList<Class> getClassesFromDirectory(String pkg, ClassLoader cl)
        throws ClassNotFoundException {
    // This will hold a list of directories matching the pckgname. There may be more than one if a package is split over multiple jars/paths
    String pckgname = pkg;/*from  w ww.  j a va 2s. com*/
    ArrayList<File> directories = new ArrayList<File>();
    try {
        String path = pckgname.replace('.', '/');
        // Ask for all resources for the path
        Enumeration<URL> resources = cl.getResources(path);
        while (resources.hasMoreElements()) {
            directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(), "UTF-8")));
        }
    } catch (UnsupportedEncodingException e) {
        if (log.isDebugEnabled()) {
            log.debug(pckgname + " does not appear to be a valid package (Unsupported encoding)");
        }
        throw new ClassNotFoundException(Messages.getMessage("ClassUtilsErr2", pckgname));
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug("IOException was thrown when trying to get all resources for " + pckgname);
        }
        throw new ClassNotFoundException(Messages.getMessage("ClassUtilsErr3", pckgname));
    }

    ArrayList<Class> classes = new ArrayList<Class>();
    // For every directory identified capture all the .class files
    for (File directory : directories) {
        if (log.isDebugEnabled()) {
            log.debug("  Adding JAXB classes from directory: " + directory.getName());
        }
        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (String file : files) {
                // we are only interested in .class files
                if (file.endsWith(".class")) {
                    // removes the .class extension
                    // TODO Java2 Sec
                    String className = pckgname + '.' + file.substring(0, file.length() - 6);
                    try {
                        Class clazz = forName(className, false, getContextClassLoader());
                        // Don't add any interfaces or JAXWS specific classes.  
                        // Only classes that represent data and can be marshalled 
                        // by JAXB should be added.
                        if (!clazz.isInterface()
                                && (clazz.isEnum() || getAnnotation(clazz, XmlType.class) != null
                                        || ClassUtils.getDefaultPublicConstructor(clazz) != null)
                                && !ClassUtils.isJAXWSClass(clazz) && !isSkipClass(clazz)
                                && !java.lang.Exception.class.isAssignableFrom(clazz)) {

                            // Ensure that all the referenced classes are loadable too
                            clazz.getDeclaredMethods();
                            clazz.getDeclaredFields();

                            if (log.isDebugEnabled()) {
                                log.debug("Adding class: " + file);
                            }
                            classes.add(clazz);

                            // REVIEW:
                            // Support of RPC list (and possibly other scenarios) requires that the array classes should also be present.
                            // This is a hack until we can determine how to get this information.

                            // The arrayName and loadable name are different.  Get the loadable
                            // name, load the array class, and add it to our list
                            //className += "[]";
                            //String loadableName = ClassUtils.getLoadableClassName(className);

                            //Class aClazz = Class.forName(loadableName, false, Thread.currentThread().getContextClassLoader());
                        }
                        //Catch Throwable as ClassLoader can throw an NoClassDefFoundError that
                        //does not extend Exception
                    } catch (Throwable e) {
                        if (log.isDebugEnabled()) {
                            log.debug("Tried to load class " + className
                                    + " while constructing a JAXBContext.  This class will be skipped.  Processing Continues.");
                            log.debug("  The reason that class could not be loaded:" + e.toString());
                            log.trace(JavaUtils.stackToString(e));
                        }
                    }

                }
            }
        }
    }

    return classes;
}

From source file:org.apache.axis2.client.Options.java

/**
 * Restore the contents of the MessageContext that was
 * previously saved.//  w w  w .j a  v a 2s  . c  o m
 * <p/>
 * NOTE: The field data must read back in the same order and type
 * as it was written.  Some data will need to be validated when
 * resurrected.
 *
 * @param in The stream to read the object contents from
 * @throws IOException
 * @throws ClassNotFoundException
 */
public void readExternal(ObjectInput inObject) throws IOException, ClassNotFoundException {
    SafeObjectInputStream in = SafeObjectInputStream.install(inObject);
    // serialization version ID
    long suid = in.readLong();

    // revision ID
    int revID = in.readInt();

    // make sure the object data is in a version we can handle
    if (suid != serialVersionUID) {
        throw new ClassNotFoundException(ExternalizeConstants.UNSUPPORTED_SUID);
    }

    // make sure the object data is in a revision level we can handle
    if (revID != REVISION_2) {
        throw new ClassNotFoundException(ExternalizeConstants.UNSUPPORTED_REVID);
    }

    //---------------------------------------------------------
    // various simple fields
    //---------------------------------------------------------
    timeOutInMilliSeconds = in.readLong();

    manageSession = in.readBoolean();

    isExceptionToBeThrownOnSOAPFault = (Boolean) in.readObject();
    useSeparateListener = (Boolean) in.readObject();

    //---------------------------------------------------------
    // various strings
    //---------------------------------------------------------

    // String soapVersionURI
    soapVersionURI = (String) in.readObject();

    // String action
    action = (String) in.readObject();

    // String transportInProtocol
    transportInProtocol = (String) in.readObject();

    // String messageId
    messageId = (String) in.readObject();

    // String object id
    logCorrelationIDString = (String) in.readObject();

    // trace point
    if (log.isTraceEnabled()) {
        log.trace(myClassName + ":readExternal():  reading the input stream for  [" + logCorrelationIDString
                + "]");
    }

    //---------------------------------------------------------
    // various objects
    //---------------------------------------------------------

    // EndpointReference faultTo
    faultTo = (EndpointReference) in.readObject();

    // EndpointReference from
    from = (EndpointReference) in.readObject();

    // EndpointReference replyTo
    replyTo = (EndpointReference) in.readObject();

    // EndpointReference to
    to = (EndpointReference) in.readObject();

    // TransportListener listener
    // is not usable until the meta data has been reconciled
    listener = null;
    metaListener = (MetaDataEntry) in.readObject();

    // TransportInDescription transportIn
    // is not usable until the meta data has been reconciled
    transportIn = null;
    metaTransportIn = (MetaDataEntry) in.readObject();

    // TransportOutDescription transportOut
    // is not usable until the meta data has been reconciled
    transportOut = null;
    metaTransportOut = (MetaDataEntry) in.readObject();

    //---------------------------------------------------------
    // collections and lists
    //---------------------------------------------------------

    // List relationships, which is an array of RelatesTo objects
    relationships = in.readArrayList();

    // ArrayList referenceParameters
    referenceParameters = in.readArrayList();

    //---------------------------------------------------------
    // properties
    //---------------------------------------------------------

    // HashMap properties
    properties = in.readHashMap();

    //---------------------------------------------------------
    // "nested"
    //---------------------------------------------------------

    // Options parent
    in.readUTF(); // read marker
    parent = (Options) in.readObject();
}

From source file:co.cask.cdap.common.conf.Configuration.java

/**
 * Load a class by name./*  ww  w. jav  a 2s  .c  om*/
 *
 * @param name the class name.
 * @return the class object.
 * @throws ClassNotFoundException if the class is not found.
 */
public Class<?> getClassByName(String name) throws ClassNotFoundException {
    Class<?> ret = getClassByNameOrNull(name);
    if (ret == null) {
        throw new ClassNotFoundException("Class " + name + " not found");
    }
    return ret;
}

From source file:org.apache.catalina.loader.WebappClassLoader.java

/**
 * Find specified class in local repositories.
 *
 * @return the loaded class, or null if the class isn't found
 */// w  w  w .j  ava 2 s. com
protected Class findClassInternal(String name) throws ClassNotFoundException {

    if (!validate(name))
        throw new ClassNotFoundException(name);

    String tempPath = name.replace('.', '/');
    String classPath = tempPath + ".class";

    ResourceEntry entry = null;

    entry = findResourceInternal(name, classPath);

    if ((entry == null) || (entry.binaryContent == null))
        throw new ClassNotFoundException(name);

    Class clazz = entry.loadedClass;
    if (clazz != null)
        return clazz;

    // Looking up the package
    String packageName = null;
    int pos = name.lastIndexOf('.');
    if (pos != -1)
        packageName = name.substring(0, pos);

    Package pkg = null;

    if (packageName != null) {

        pkg = getPackage(packageName);

        // Define the package (if null)
        if (pkg == null) {
            if (entry.manifest == null) {
                definePackage(packageName, null, null, null, null, null, null, null);
            } else {
                definePackage(packageName, entry.manifest, entry.codeBase);
            }
        }

    }

    // Create the code source object
    CodeSource codeSource = new CodeSource(entry.codeBase, entry.certificates);

    if (securityManager != null) {

        // Checking sealing
        if (pkg != null) {
            boolean sealCheck = true;
            if (pkg.isSealed()) {
                sealCheck = pkg.isSealed(entry.codeBase);
            } else {
                sealCheck = (entry.manifest == null) || !isPackageSealed(packageName, entry.manifest);
            }
            if (!sealCheck)
                throw new SecurityException(
                        "Sealing violation loading " + name + " : Package " + packageName + " is sealed.");
        }

    }

    if (entry.loadedClass == null) {
        synchronized (this) {
            if (entry.loadedClass == null) {
                clazz = defineClass(name, entry.binaryContent, 0, entry.binaryContent.length, codeSource);
                entry.loadedClass = clazz;
                entry.binaryContent = null;
                entry.source = null;
                entry.codeBase = null;
                entry.manifest = null;
                entry.certificates = null;
            } else {
                clazz = entry.loadedClass;
            }
        }
    } else {
        clazz = entry.loadedClass;
    }

    return clazz;

}

From source file:org.wso2.carbon.context.internal.CarbonContextDataHolder.java

private static Class<?> classForName(final String className) throws ClassNotFoundException {

    Class<?> cls = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
        public Class<?> run() {
            // try thread context class loader first
            try {
                return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
            } catch (ClassNotFoundException ignored) {
                if (log.isDebugEnabled()) {
                    log.debug(ignored);//w ww  .  j a v a 2 s. co m
                }

            }
            // try system class loader second
            try {
                return Class.forName(className, true, ClassLoader.getSystemClassLoader());
            } catch (ClassNotFoundException ignored) {
                if (log.isDebugEnabled()) {
                    log.debug(ignored);
                }
            }
            // return null, if fail to load class
            return null;
        }
    });

    if (cls == null) {
        throw new ClassNotFoundException("class " + className + " not found");
    }

    return cls;
}

From source file:com.rapidminer.tools.Tools.java

/** TODO: Looks like this can be replaced by {@link Plugin#getMajorClassLoader()} */
public static Class<?> classForName(String className) throws ClassNotFoundException {
    try {//from w w w .  j  a  va2  s.c o  m
        return Class.forName(className);
    } catch (ClassNotFoundException e) {
    }
    try {
        return ClassLoader.getSystemClassLoader().loadClass(className);
    } catch (ClassNotFoundException e) {
    }
    Iterator<Plugin> i = Plugin.getAllPlugins().iterator();
    while (i.hasNext()) {
        Plugin p = i.next();
        try {
            return p.getClassLoader().loadClass(className);
        } catch (ClassNotFoundException e) {
            // this wasn't it, so continue
        }
    }
    throw new ClassNotFoundException(className);
}