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:de.fau.cs.osr.utils.NameAbbrevService.java

/**
 * Resolves an abbreviated class name to the corresponding Class<?> object.
 * /*from   w w  w .  ja  v  a2 s  . com*/
 * @throws ClassNotFoundException
 *             Thrown if the abbreviated class cannot be found in any
 *             package of the package list.
 */
public Class<?> resolve(String abbrev) throws ClassNotFoundException {
    int dim = getArrayDim(abbrev);

    abbrev = abbrev.substring(0, abbrev.length() - dim * 2);

    Class<?> clazz = (Class<?>) cache.getKey(abbrev);
    if (clazz != null)
        return arrayClassFor(clazz, dim);

    if (abbrev.indexOf('.') >= 0) {
        // Full name was given
        clazz = Class.forName(abbrev);
        cache.put(clazz, abbrev);
        return arrayClassFor(clazz, dim);
    }

    final String dotSimpleName = "." + abbrev;
    for (String pkg : packages) {
        try {
            clazz = Class.forName(pkg + dotSimpleName);
            cache.put(clazz, abbrev);
            return arrayClassFor(clazz, dim);
        } catch (ClassNotFoundException e) {
        }
    }

    throw new ClassNotFoundException(
            "Given abbreviated class name was " + "not found in any package of the package list: " + abbrev);
}

From source file:org.springframework.security.config.SecurityNamespaceHandlerTests.java

@Test
public void websocketNotFoundExceptionNoMessageBlock() throws Exception {
    String className = FILTER_CHAIN_PROXY_CLASSNAME;
    spy(ClassUtils.class);
    doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName",
            eq(Message.class.getName()), any(ClassLoader.class));
    new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
    // should load just fine since no websocket block
}

From source file:com.googlecode.onevre.utils.ServerClassLoader.java

/**
 *
 * @see java.lang.ClassLoader#findClass(java.lang.String)
 */// w w  w.j a  v  a2  s.c  om
protected Class<?> findClass(String name) throws ClassNotFoundException {

    // Attempt to find the class in a cached jar file
    String pathName = name.replaceAll("\\.", "/") + ".class";
    try {
        URL url = getResourceURL(pathName);
        if (url != null) {
            Class<?> loadedClass = defineClassFromJar(name, url, cachedJars.get(url), pathName);
            return loadedClass;
        }
        throw new ClassNotFoundException("Could not find class " + name);
    } catch (IOException e) {
        throw new ClassNotFoundException("Error finding class " + name, e);
    }
}

From source file:lucee.commons.lang.PhysicalClassLoader.java

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    Resource res = directory.getRealResource(name.replace('.', '/').concat(".class"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//  ww  w . j  ava2  s . co  m
        IOUtil.copy(res, baos, false);
    } catch (IOException e) {//e.printStackTrace();
        throw new ClassNotFoundException("class " + name + " is invalid or doesn't exist");
    }

    byte[] barr = baos.toByteArray();
    size += barr.length;
    count++;
    //print.o(name+":"+count+" -> "+(size/1024));
    IOUtil.closeEL(baos);
    return loadClass(name, barr);
    //return defineClass(name,barr,0,barr.length);
}

From source file:czlab.wabbit.CljPodLoader.java

@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
    Class<?> clazz = null;/*from   w ww  . j a va  2 s.co  m*/
    //TLOG.info("findClass##### {}", name);
    if (_transformers.isEmpty()) {
        clazz = super.findClass(name);
    } else {
        String path = name.replace('.', '/').concat(".class");
        URL url = getResource(path);
        if (url == null) {
            throw new ClassNotFoundException(name);
        }

        try (InputStream content = url.openStream()) {
            byte[] bytes = IOUtils.toByteArray(content);
            for (ClassFileTransformer t : _transformers) {
                byte[] tmp = t.transform(this, name, null, null, bytes);
                if (tmp != null) {
                    bytes = tmp;
                }
            }
            clazz = defineClass(name, bytes, 0, bytes.length);
        } catch (IOException e) {
            throw new ClassNotFoundException(name, e);
        } catch (IllegalClassFormatException e) {
            throw new ClassNotFoundException(name, e);
        }
    }
    return clazz;
}

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

/**
 * Find class inside managed jars or hand over the parent
 * @see java.lang.ClassLoader#findClass(java.lang.String)
 *//*  www .ja v a  2 s .  c  o m*/
protected Class<?> findClass(String name) throws ClassNotFoundException {

    // find in already loaded classes to make things shorter
    Class<?> clazz = findLoadedClass(name);
    if (clazz != null)
        return clazz;

    // check if the class searched for is contained inside managed jars,
    // otherwise hand over the request to the parent class loader
    String jarFileName = this.classesJarMapping.get(name);
    if (StringUtils.isBlank(jarFileName)) {
        super.findClass(name);
    }

    // try to find class inside jar the class name is associated with
    JarInputStream jarInput = null;
    try {
        // open a stream on jar which contains the class
        jarInput = new JarInputStream(new FileInputStream(jarFileName));
        // ... and iterate through all entries
        JarEntry jarEntry = null;
        while ((jarEntry = jarInput.getNextJarEntry()) != null) {

            // extract the name of the jar entry and check if it has suffix '.class' and thus contains
            // the search for implementation
            String entryFileName = jarEntry.getName();
            if (entryFileName.endsWith(".class")) {

                // replace slashes by dots, remove suffix and compare the result with the searched for class name 
                entryFileName = entryFileName.substring(0, entryFileName.length() - 6).replace('/', '.');
                if (name.equalsIgnoreCase(entryFileName)) {

                    // load bytes from jar entry and define a class over it
                    byte[] data = loadBytes(jarInput);
                    if (data != null) {
                        return defineClass(name, data, 0, data.length);
                    }

                    // if the jar entry does not contain any data, throw an exception
                    throw new ClassNotFoundException("Class '" + name + "' not found");
                }
            }
        }
    } catch (IOException e) {
        // if any io error occurs: throw an exception
        throw new ClassNotFoundException("Class '" + name + "' not found");
    } finally {
        try {
            jarInput.close();
        } catch (IOException e) {
            logger.error("Failed to close open JAR file '" + jarFileName + "'. Error: " + e.getMessage());
        }
    }

    // if no such class exists, throw an exception
    throw new ClassNotFoundException("Class [" + name + "] not found");

}

From source file:com.sshtools.j2ssh.util.ExtensionClassLoader.java

public byte[] loadClassData(String name) throws ClassNotFoundException {
    // Try to load it from each classpath
    Iterator it = classpath.iterator();

    // Cache entry.
    ClassCacheEntry classCache = new ClassCacheEntry();

    while (it.hasNext()) {
        byte[] classData;

        File file = (File) it.next();

        try {//  ww  w  .java 2 s.c o  m
            if (file.isDirectory()) {
                classData = loadClassFromDirectory(file, name, null);
            } else {
                classData = loadClassFromZipfile(file, name, null);
            }
        } catch (IOException ioe) {
            // Error while reading in data, consider it as not found
            classData = null;
        }

        if (classData != null) {
            return classData;
        }
    }

    // If not found in any classpath
    throw new ClassNotFoundException(name);

}

From source file:de.micromata.genome.gwiki.plugin.GWikiPluginJavaClassLoader.java

@Override
public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {

    if (loadedClasses.containsKey(name) == true) {
        return loadedClasses.get(name);
    }/*from  www .  ja va2 s.c  o  m*/

    if (enableCacheMissedClasses == true && missedClasses.contains(name) == true) {
        throw new ClassNotFoundException("Class " + name + " cannot be found (cached)");
    }

    String clsName = name.replace('.', '/') + ".class";
    for (Map<String, FsObject> rsm : resPaths) {
        if (rsm.containsKey(clsName) == false) {
            continue;
        }
        FsObject sclr = rsm.get(clsName);
        byte[] data = sclr.getFileSystem().readBinaryFile(sclr.getName());

        // TODO problem. If isolated, this needs the complete class loader.
        // super should not be the complete config repository, but only required.
        Class<?> cls = loadDefine(name, data);
        return cls;

    }
    if (isolated == true) {
        throw new ClassNotFoundException("Cannot find class: " + name);
    }
    try {
        Class<?> cls = super.loadClass(name, resolve);
        if (enableCacheMissedClasses == true && cls == null) {
            missedClasses.add(name);
        }
        return cls;
    } catch (ClassNotFoundException ex) {
        if (enableCacheMissedClasses == true) {
            missedClasses.add(name);
        }
        throw ex;
    }
}

From source file:org.squidy.common.dynamiccode.DynamicCodeClassLoader.java

/**
 * Returns the up-to-date dynamic class by name.
 * //from  w w w .  jav  a 2s.com
 * @param className
 * @return
 * @throws ClassNotFoundException if source file not found or compilation error
 */
public Class<?> loadClass(String className) throws ClassNotFoundException {

    LoadedClass loadedClass = null;
    synchronized (loadedClasses) {
        loadedClass = (LoadedClass) loadedClasses.get(className);
    }

    // first access of a class
    if (loadedClass == null) {

        String resource = className.replace('.', '/') + ".java";
        SourceDir src = locateResource(resource);
        if (src == null) {
            try {
                return parentClassLoader.loadClass(className);
            } catch (Exception e) {
                throw new ClassNotFoundException("DynamicCode class not found " + className);
            }
        }

        synchronized (this) {

            // compile and load class
            loadedClass = new LoadedClass(className, src);

            synchronized (loadedClasses) {
                loadedClasses.put(className, loadedClass);
            }
        }

        return loadedClass.loadedClass;
    }

    // subsequent access
    if (loadedClass.isChanged()) {
        // unload and load again
        unload(loadedClass.srcDir);
        return loadClass(className);
    }

    return loadedClass.loadedClass;
}

From source file:com.seeburger.vfs2.util.VFSClassLoader.java

/**
 * Finds and loads the class with the specified name from the search
 * path.//from   w ww. j  a va 2 s .  c o m
 *
 * @throws ClassNotFoundException if the class is not found.
 */
@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
    try {
        final String path = name.replace('.', '/').concat(".class");
        final Resource res = loadResource(path);
        if (res == null) {
            throw new ClassNotFoundException(name);
        }
        return defineClass(name, res);
    } catch (final IOException ioe) {
        throw new ClassNotFoundException(name, ioe);
    }
}