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.impalaframework.classloader.CustomClassLoader.java

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

    Class<?> toReturn = loadParentClass(className);

    if (toReturn == null) {
        toReturn = getAlreadyLoadedClass(className);
    }/*from w w w .j av  a 2s.  co  m*/
    if (toReturn == null) {
        toReturn = loadCustomClass(className);
    }
    if (toReturn == null) {
        if (logger.isDebugEnabled())
            logger.debug("Class not found: " + className);
        throw new ClassNotFoundException(className);
    }
    return toReturn;
}

From source file:org.impalaframework.interactive.classloader.TestClassLoader.java

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

    Class<?> toReturn = null;

    if (!className.contains(testClassName)) {
        toReturn = loadParentClass(className);
        if (toReturn == null) {
            toReturn = getAlreadyLoadedClass(className);
        }/*from  www. j  a v a2  s  .  com*/
    }

    if (toReturn == null) {
        toReturn = loadCustomClass(className);
    }

    if (toReturn == null) {
        if (logger.isDebugEnabled())
            logger.debug("Class not found: " + className);
        throw new ClassNotFoundException(className);
    }

    // System.out.println("TestClassLoader: " + className + " loaded by " +
    // toReturn.getClassLoader());
    return toReturn;

}

From source file:org.apache.openaz.xacml.util.FactoryFinder.java

/**
 * Attempts to load a class using the given <code>ClassLoader</code>. If that fails and fallback is
 * enabled, the current <code>ClassLoader</code> is tried. If the <code>ClassLoader</code> is null, use
 * the context <code>ClassLoader</code> followed by the current <code>ClassLoader</code>.
 *
 * @param className the <code>String</code> name of the <code>Class</code> to load
 * @param cl the <code>ClassLoader</code> to use
 * @param doFallback if true, fall back to the current <code>ClassLoader</code> if the given
 *            <code>ClassLoader</code> fails
 * @return the <code>Class</code> for the given class name
 * @throws ClassNotFoundException if the <code>Class</code> cannot be found
 */// w  ww.  j av a 2  s.  c o  m
private static Class<?> getProviderClass(String className, ClassLoader cl, boolean doFallback)
        throws ClassNotFoundException {
    try {
        if (cl == null) {
            cl = Thread.class.getClassLoader();
            if (cl == null) {
                cl = FactoryFinder.class.getClassLoader();
                if (cl == null) {
                    throw new ClassNotFoundException("No ClassLoader() in current context");
                } else {
                    return cl.loadClass(className);
                }
            } else {
                return cl.loadClass(className);
            }
        } else {
            return cl.loadClass(className);
        }
    } catch (ClassNotFoundException ex) {
        if (doFallback) {
            return Class.forName(className, true, FactoryFinder.class.getClassLoader());
        } else {
            throw ex;
        }
    }
}

From source file:org.impalaframework.classloader.ModuleTestClassLoader.java

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

    Class<?> toReturn = loadCustomClass(className);

    if (toReturn == null) {
        toReturn = loadParentClass(className);
    }//from ww  w . j a va2 s . c o  m

    if (toReturn == null) {
        toReturn = getAlreadyLoadedClass(className);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Loaded " + " class " + className + ": " + toReturn.getClassLoader());
    }

    if (toReturn == null) {
        if (logger.isDebugEnabled())
            logger.debug("Class not found: " + className);
        throw new ClassNotFoundException(className);
    }

    return toReturn;

}

From source file:com.wavemaker.tools.util.CFClassLoader.java

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {

    if (this.resources == null) {
        throw new ClassNotFoundException("invalid search root: " + this.resources);
    } else if (name == null) {
        throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage());
    }/*from   w ww .  ja v  a2 s.c om*/

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

    List<Resource> files = new ArrayList<Resource>();
    for (int i = 0; i < this.resources.length; i++) {
        files.addAll(this.fileSystem.listAllChildren(this.resources[i], null));
    }

    byte[] fileBytes = null;
    try {
        InputStream is = null;
        JarFile jarFile = null;

        for (Resource entry : files) {
            String resourcePath = getPath(entry);
            int len1 = resourcePath.length();
            int len2 = classNamePath.length();
            if (len1 > len2) {
                if (resourcePath.substring(len1 - len2, len1).equals(classNamePath)) {
                    is = entry.getInputStream();
                    break;
                }
            }
        }

        if (is != null) {
            try {
                fileBytes = IOUtils.toByteArray(is);
                is.close();
            } finally {
                if (jarFile != null) {
                    jarFile.close();
                }
            }
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(e.getMessage(), e);
    }

    if (name.contains(".")) {
        String packageName = name.substring(0, name.lastIndexOf('.'));
        if (getPackage(packageName) == null) {
            definePackage(packageName, "", "", "", "", "", "", null);
        }
    }

    Class<?> ret;
    try {
        if (fileBytes == null) {
            ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader);
        } else {
            ret = defineClass(name, fileBytes, 0, fileBytes.length);
        }
    } catch (WMRuntimeException ex) {
        ret = null;
    }

    if (ret == null) {
        throw new ClassNotFoundException(
                "Couldn't find class " + name + " in expected classpath: " + this.resources);
    }

    return ret;
}

From source file:org.apache.logging.log4j.core.selector.TestClassLoader.java

@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
    final String path = name.replace('.', '/').concat(".class");
    final URL resource = super.getResource(path);
    if (resource == null) {
        throw new ClassNotFoundException(name);
    }/*w w  w  . j a  va  2s  .  c om*/
    try {
        final URLConnection uc = resource.openConnection();
        final int len = uc.getContentLength();
        final InputStream in = new BufferedInputStream(uc.getInputStream());
        final byte[] bytecode = new byte[len];
        try {
            IOUtils.readFully(in, bytecode);
        } finally {
            Closer.closeSilently(in);
        }
        return defineClass(name, bytecode, 0, bytecode.length);
    } catch (final IOException e) {
        Throwables.rethrow(e);
        return null; // unreachable
    }
}

From source file:com.opensymphony.webwork.util.classloader.stores.ResourceStoreClassLoader.java

protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
    Class clazz = findLoadedClass(name);

    if (clazz == null) {
        clazz = fastFindClass(name);/*from  w w  w  .  ja v  a  2  s .c o  m*/

        if (clazz == null) {

            final ClassLoader parent = getParent();
            if (parent != null) {
                clazz = parent.loadClass(name);
                log.debug("loaded from parent: " + name);
            } else {
                throw new ClassNotFoundException(name);
            }

        } else {
            log.debug("loaded from store: " + name);
        }
    }

    if (resolve) {
        resolveClass(clazz);
    }

    return clazz;
}

From source file:com.fuzhepan.arpc.client.ProxyHandler.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    log.debug("invoke was called!");

    if (method.getName().equals("toString")) {
        return "toString method was called";
    }//from  ww w.  j  ava  2s  .c o  m

    RpcContext rpcContext = new RpcContext(interfaceName, method.getName(), method.getParameterTypes(), args);

    //get service info and load balance
    List<HostPortPair> serviceList = RpcConfig.getServiceList(serviceName);
    if (serviceList == null || serviceList.size() == 0)
        throw new ClassNotFoundException("not find service : " + serviceName);
    int index = requestCount.get() % serviceList.size();
    if (requestCount.get() > 100)
        requestCount.set(0);
    else
        requestCount.getAndIncrement();
    HostPortPair hostPort = serviceList.get(index);

    Socket socket = new Socket(hostPort.host, hostPort.port);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
    objectOutputStream.writeObject(rpcContext);
    objectOutputStream.flush();

    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
    Object response = objectInputStream.readObject();

    objectInputStream.close();
    objectOutputStream.close();
    socket.close();

    Class methodReturnType = method.getReturnType();
    return methodReturnType.cast(response);
}

From source file:com.wavemaker.commons.classloader.ThrowawayFileClassLoader.java

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {

    if (this.classPath == null) {
        throw new ClassNotFoundException("invalid search root: " + this.classPath);
    } else if (name == null) {
        throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage());
    }/*w  w  w  .j  a  v a 2  s .  c o  m*/

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

    byte[] fileBytes = null;
    try {
        InputStream is = null;
        JarFile jarFile = null;

        for (Resource entry : this.classPath) {
            if (entry.getFilename().toLowerCase().endsWith(".jar")) {
                jarFile = new JarFile(entry.getFile());
                ZipEntry ze = jarFile.getEntry(classNamePath);

                if (ze != null) {
                    is = jarFile.getInputStream(ze);
                    break;
                } else {
                    jarFile.close();
                }
            } else {
                Resource classFile = entry.createRelative(classNamePath);
                if (classFile.exists()) {
                    is = classFile.getInputStream();
                    break;
                }
            }
        }

        if (is != null) {
            try {
                fileBytes = IOUtils.toByteArray(is);
                is.close();
            } finally {
                if (jarFile != null) {
                    jarFile.close();
                }
            }
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(e.getMessage(), e);
    }

    if (name.contains(".")) {
        String packageName = name.substring(0, name.lastIndexOf('.'));
        if (getPackage(packageName) == null) {
            definePackage(packageName, "", "", "", "", "", "", null);
        }
    }

    Class<?> ret;
    if (fileBytes == null) {
        ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader);
    } else {
        ret = defineClass(name, fileBytes, 0, fileBytes.length);
    }

    if (ret == null) {
        throw new ClassNotFoundException(
                "Couldn't find class " + name + " in expected classpath: " + this.classPath);
    }

    return ret;
}

From source file:com.github.trask.sandbox.isolation.ClassLoaderExtension.java

Class<?> findClass(String name) throws ClassNotFoundException {
    if (bridgeInterface != null && bridgeInterface.getName().equals(name)) {
        return bridgeInterface;
    }//from   w  w w  . j  ava 2  s.c  om
    String resourceName = name.replace('.', '/') + ".class";
    InputStream input = extensibleClassLoader.getResourceAsStream(resourceName);
    if (input == null) {
        throw new ClassNotFoundException(name);
    }
    byte[] b;
    try {
        b = IOUtils.toByteArray(input);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    if (name.indexOf('.') != -1) {
        String packageName = StringUtils.substringBeforeLast(name, ".");
        extensibleClassLoader.createPackageIfNecessary(packageName);
    }
    try {
        return extensibleClassLoader.defineClass(name, b);
    } catch (IOException e) {
        throw new ClassNotFoundException(name, e);
    }
}