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, Throwable ex) 

Source Link

Document

Constructs a ClassNotFoundException with the specified detail message and optional exception that was raised while loading the class.

Usage

From source file:wicket.contrib.groovy.GroovyWarClassLoader.java

/**
 * First try parent's implementation. If it fails, it'll throw a
 * ClassCastException. Catch it and than apply additional means to load the
 * class./*  ww  w  .j a v a 2 s  .c o m*/
 * 
 * @param name
 *            Class name incl. package
 * @return The Class loaded, if found
 * @throws ClassNotFoundException,
 *             if class could not be loaded
 */
protected Class findClass(final String name) throws ClassNotFoundException {
    try {
        // Try Groovies default implementation first
        return super.findClass(name);
    } catch (ClassNotFoundException ex) {
        log.debug("class name: " + name);

        // classname => filename
        String filename = Strings.replaceAll(name, ".", "/") + ".groovy";

        // File exists?
        final URL url = getResource(filename);
        if (url != null) {
            try {
                // Get Groovy to parse the file and create the Class
                final InputStream in = url.openStream();
                if (in != null) {
                    Class clazz = parseClass(in);
                    if (clazz != null) {
                        return clazz;
                    }
                } else {
                    log.warn("Groovy file not found: " + filename);
                }
            } catch (CompilationFailedException e) {
                throw new ClassNotFoundException("Error parsing groovy file: " + filename, e);
            } catch (IOException e) {
                throw new ClassNotFoundException("Error reading groovy file: " + filename, e);
            } catch (Throwable e) {
                throw new ClassNotFoundException("Error while reading groovy file: " + filename, e);
            }
        }

        throw ex;
    }
}

From source file:org.evosuite.instrumentation.InstrumentingClassLoader.java

public Class<?> loadClassFromFile(String fullyQualifiedTargetClass, String fileName)
        throws ClassNotFoundException {

    String className = fullyQualifiedTargetClass.replace('.', '/');
    InputStream is = null;/*  ww w. j a  v  a  2s .  c  o  m*/
    try {
        is = new FileInputStream(new File(fileName));
        byte[] byteBuffer = instrumentation.transformBytes(this, className, new ClassReader(is));
        createPackageDefinition(fullyQualifiedTargetClass);
        Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length);
        classes.put(fullyQualifiedTargetClass, result);
        logger.info("Keeping class: {}", fullyQualifiedTargetClass);
        return result;
    } catch (Throwable t) {
        logger.info("Error while loading class: {}", t);
        throw new ClassNotFoundException(t.getMessage(), t);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                throw new Error(e);
            }
    }
}

From source file:spring.osgi.context.internal.classloader.BundleDelegatingClassLoader.java

protected Class findClass(String name) throws ClassNotFoundException {
    try {/*from  w ww . j  a va  2s .  c  o m*/
        return this.backingBundle.loadClass(name);
    } catch (ClassNotFoundException cnfe) {

        throw new ClassNotFoundException(
                name + " not found from bundle [" + backingBundle.getSymbolicName() + "]", cnfe);
    } catch (NoClassDefFoundError ncdfe) {
        // This is almost always an error
        // This is caused by a dependent class failure,
        // so make sure we search for the right one.
        NoClassDefFoundError e = new NoClassDefFoundError(
                name + " not found from bundle [" + backingBundle + "]");
        e.initCause(ncdfe);
        throw e;
    }
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloVFSClassLoader.java

public synchronized static <U> Class<? extends U> loadClass(String classname, Class<U> extension)
        throws ClassNotFoundException {
    try {/*from  ww  w. ja va2 s . com*/
        return getClassLoader().loadClass(classname).asSubclass(extension);
    } catch (IOException e) {
        throw new ClassNotFoundException("IO Error loading class " + classname, e);
    }
}

From source file:rapture.common.jar.AbstractClassLoader.java

@Override
protected Class<?> findClass(String className) throws ClassNotFoundException {
    log.debug("findClass() for className: " + className);
    String jarUri = classNameMap.get(className);
    if (StringUtils.isBlank(jarUri)) {
        log.debug("Not found, delegating to parent: " + className);
        return super.findClass(className);
    }/*from   ww w .j  av  a2 s.  c om*/
    try {
        byte[] classBytes = JarCache.getInstance().getClassBytes(api, jarUri, className);
        return defineClass(className, classBytes, 0, classBytes.length);
    } catch (ExecutionException e) {
        throw new ClassNotFoundException("Could not get class bytes from the cache", e);
    }
}

From source file:org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedWebappClassLoader.java

private void checkPackageAccess(String name) throws ClassNotFoundException {
    if (this.securityManager != null && name.lastIndexOf('.') >= 0) {
        try {//from w  w w.ja va 2  s .co m
            this.securityManager.checkPackageAccess(name.substring(0, name.lastIndexOf('.')));
        } catch (SecurityException ex) {
            throw new ClassNotFoundException(
                    "Security Violation, attempt to use " + "Restricted Class: " + name, ex);
        }
    }
}

From source file:gridool.deployment.PeerClassLoader.java

@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
    BlockingQueue<GridTaskResult> resultQueue = new SynchronousQueue<GridTaskResult>();
    String jobId = "p2p-classloading_" + resultQueue.hashCode();
    responseQueue.addResponseQueue(jobId, resultQueue);

    if (LOG.isInfoEnabled()) {
        LOG.info("Loading a class '" + name + "' from " + remoteNode);
    }/*from  w  ww.j a v a2  s  .  c  o  m*/

    GridNode localNode = communicator.getLocalNode();
    final GridGetClassTask task = new GridGetClassTask(jobId, localNode, name);
    try {// send a class-loading request
        communicator.sendTaskRequest(task, remoteNode);
    } catch (GridException e) {
        throw new ClassNotFoundException("Failed sending a GridGetClassTask of the class: " + name, e);
    }

    // Receive a requested class
    final GridTaskResult result;
    try {
        result = resultQueue.take(); // TODO timeout
    } catch (InterruptedException e) {
        throw new ClassNotFoundException("An error caused while receiving a class: " + name, e);
    }
    final ClassData clazz = result.getResult();
    assert (clazz != null);
    byte[] clazzData = clazz.getClassData();
    long ts = clazz.getTimestamp();
    final Class<?> definedClazz = parentLdr.defineClassIfNeeded(name, clazzData, ts);

    // define enclosing classes
    ClassData enclosingClass = clazz.getEnclosingClass();
    while (enclosingClass != null) {
        defineClass(enclosingClass, parentLdr);
        enclosingClass = enclosingClass.getEnclosingClass();
    }

    return definedClazz;
}

From source file:de.smartics.maven.plugin.jboss.modules.util.classpath.AbstractProjectClassLoader.java

/**
 * Loads the class. Ensures that the package of the class is defined.
 *
 * @param className the name of the class.
 * @param classFile the file with the binary data.
 * @return the loaded class definition.//w  ww.  j  a v  a2s .  c o m
 * @throws ClassNotFoundException if the class cannot be loaded.
 */
protected Class<?> loadClassFile(final String className, final File classFile) throws ClassNotFoundException {
    ensurePackageProvided(className);
    InputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(classFile));
        final byte[] data = IOUtils.toByteArray(in);
        final Class<?> clazz = defineClass(className, data, 0, data.length);
        return clazz;
    } catch (final IOException e) {
        final String message = "Cannot load class '" + className + "' from file '" + classFile + "'.";
        LOG.fine(message);

        throw new ClassNotFoundException(message, e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:streaming.common.HdfsClassLoader.java

/**
 * Search for the class in the configured jar file stored in HDFS.
 *
 * {@inheritDoc}/*from www . j  a  v a  2 s  .com*/
 */
@Override
public Class findClass(String className) throws ClassNotFoundException {
    String classPath = convertNameToPath(className);
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Searching for class %s (%s) in path %s", className, classPath, this.jar));
    }
    FileSystem fs = null;
    JarInputStream jarIn = null;
    try {
        fs = this.jar.getFileSystem(this.configuration);
        jarIn = new JarInputStream(fs.open(this.jar));
        JarEntry currentEntry = null;
        while ((currentEntry = jarIn.getNextJarEntry()) != null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace(String.format("Comparing %s to entry %s", classPath, currentEntry.getName()));
            }
            if (currentEntry.getName().equals(classPath)) {
                byte[] classBytes = readEntry(jarIn);
                return defineClass(className, classBytes, 0, classBytes.length);
            }
        }
    } catch (IOException ioe) {
        throw new ClassNotFoundException("Unable to find " + className + " in path " + this.jar, ioe);
    } finally {
        closeQuietly(jarIn);
        // While you would think it would be prudent to close the filesystem that you opened,
        // it turns out that this filesystem is shared with HBase, so when you close this one,
        // it becomes closed for HBase, too.  Therefore, there is no call to closeQuietly(fs);
    }
    throw new ClassNotFoundException("Unable to find " + className + " in path " + this.jar);
}

From source file:org.shelloid.netverif.ClassTransformerClassLoader.java

public Class<?> loadClass(final String name) throws ClassNotFoundException {

    final int i = name.indexOf('$');
    final String key;

    if (i == -1) {
        key = name;/* w w w  .ja v  a  2s .  com*/
    } else {
        key = name.substring(0, i);
    }

    if (name.startsWith("org.shelloid.netverif")) {

        try {
            final InputStream is = getClass().getResourceAsStream("/" + name.replace('.', '/') + ".class");

            final byte[] bytecode;

            if (true) {
                // System.err.println("Instrumenting: " + name);
                bytecode = transform(name, is);
            } else {
                // System.err.println("Loading: " + name);
                bytecode = new ClassReader(is).b;
            }

            return super.defineClass(name, bytecode, 0, bytecode.length);

        } catch (Throwable ex) {
            // System.err.println("Load error: " + ex.toString());
            ex.printStackTrace();
            throw new ClassNotFoundException(name + " " + ex.getMessage(), ex);
        }
    }

    // System.err.println("Delegating: " + name);
    return super.loadClass(name);
}