Example usage for java.lang ClassNotFoundException getCause

List of usage examples for java.lang ClassNotFoundException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:ConstructorTroubleToo.java

public static void main(String... args) {
    try {/*from ww  w. j a va 2s  .c o  m*/
        Class<?> c = Class.forName("ConstructorTroubleToo");
        // Method propagetes any exception thrown by the constructor (including checked exceptions).
        if (args.length > 0 && args[0].equals("class")) {
            Object o = c.newInstance();
        } else {
            Object o = c.getConstructor().newInstance();
        }

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (NoSuchMethodException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
        err.format("%n%nCaught exception: %s%n", x.getCause());
    }
}

From source file:org.saiku.adhoc.utils.TemplateUtils.java

/**
 * Finds the class for a given aggreagation function name
 * /*  www.j a  va2  s.  c om*/
 * @param str
 * @return
 * @throws ReportException
 */
public static Class strToAggfunctionClass(String str) throws ReportException {

    try {

        if (str.equals("NONE")) {
            return null;
        } else if (str.equals("SUM")) {
            return Class.forName("org.pentaho.reporting.engine.classic.core.function.ItemSumFunction");
        } else if (str.equals("AVERAGE")) {
            return Class.forName("org.pentaho.reporting.engine.classic.core.function.ItemAvgFunction");
        } else if (str.equals("COUNT")) {
            return Class.forName("org.pentaho.reporting.engine.classic.core.function.ItemCountFunction");
        } else if (str.equals("COUNT_DISTINCT")) {
            return Class.forName("org.pentaho.reporting.engine.classic.core.function.CountDistinctFunction");
        } else if (str.equals("MINIMUM")) {
            return Class.forName("org.pentaho.reporting.engine.classic.core.function.ItemMinFunction");
        } else if (str.equals("MAXIMUM")) {

            return Class.forName("org.pentaho.reporting.engine.classic.core.function.ItemMaxFunction");
        }

    } catch (ClassNotFoundException e) {
        final String message = e.getCause() != null
                ? e.getCause().getClass().getName() + " - " + e.getCause().getMessage()
                : e.getClass().getName() + " - " + e.getMessage();
        log.error(message, e);
        throw new ReportException("Aggregation Class not found", e);
    }

    return null;

}

From source file:com.github.veithen.ulog.MetaFactory.java

private static LoggerFactoryBinder getLoggerFactoryBinder() {
    Class binderClass;//  w ww .j  a va2  s. c  o m
    try {
        binderClass = MetaFactory.class.getClassLoader().loadClass("org.slf4j.impl.StaticLoggerBinder");
    } catch (ClassNotFoundException ex) {
        return null;
    }
    try {
        return (LoggerFactoryBinder) binderClass.getMethod("getSingleton", new Class[0]).invoke(null,
                new Object[0]);
    } catch (InvocationTargetException ex) {
        logger.log(Level.WARNING, "Unable to get the SLF4J LoggerFactoryBinder", ex.getCause());
        return null;
    } catch (Throwable ex) {
        logger.log(Level.WARNING, "Unable to get the SLF4J LoggerFactoryBinder", ex);
        return null;
    }
}

From source file:com.he5ed.lib.cloudprovider.utils.AuthHelper.java

/**
 * Extract user information from the JSONObject
 *
 * @param cloudApi type of cloud account
 * @param jsonObject JSONObject that contain user information
 * @return User//from  www. j  a  v a2  s.  co m
 * @throws JSONException
 */
public static User extractUser(String cloudApi, JSONObject jsonObject) throws JSONException {
    // use reflection for flexibility
    try {
        Class<?> clazz = Class.forName(cloudApi);
        Method extractUser = clazz.getMethod("extractUser", JSONObject.class);
        return (User) extractUser.invoke(null, jsonObject);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        // catch exception throw by class method
        if (e.getCause() instanceof JSONException) {
            throw new JSONException(e.getMessage());
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.he5ed.lib.cloudprovider.utils.AuthHelper.java

/**
 * Extract access token from the JSONObject
 *
 * @param cloudApi type of cloud account
 * @param jsonObject JSONObject that contain access token
 * @return Map/* ww w  .ja  v  a2s . co  m*/
 * @throws JSONException
 */
public static Map<String, String> extractAccessToken(String cloudApi, JSONObject jsonObject)
        throws JSONException {
    // use reflection for flexibility
    try {
        Class<?> clazz = Class.forName(cloudApi);
        Method extractAccessToken = clazz.getMethod("extractAccessToken", JSONObject.class);
        return (Map) extractAccessToken.invoke(null, jsonObject);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        // catch exception throw by class method
        if (e.getCause() instanceof JSONException) {
            throw new JSONException(e.getMessage());
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.apache.hawq.pxf.service.utilities.Utilities.java

/**
 * Creates an object using the class name. The class name has to be a class
 * located in the webapp's CLASSPATH.//from  w w  w. j  ava  2  s  .com
 *
 * @param confClass the class of the metaData used to initialize the
 *            instance
 * @param className a class name to be initialized.
 * @param metaData input data used to initialize the class
 * @return Initialized instance of given className
 * @throws Exception throws exception if classname was not found in
 *             classpath, didn't have expected constructor or failed to be
 *             instantiated
 */
public static Object createAnyInstance(Class<?> confClass, String className, InputData metaData)
        throws Exception {

    Class<?> cls = null;
    try {
        cls = Class.forName(className);
    } catch (ClassNotFoundException e) {
        // in case the class name uses the old "com.pivotal.pxf" package
        // name, recommend using the new package "org.apache.hawq.pxf".
        if (className.startsWith("com.pivotal.pxf")) {
            throw new Exception("Class " + className + " does not appear in classpath. "
                    + "Plugins provided by PXF must start with \"org.apache.hawq.pxf\"", e.getCause());
        } else {
            throw e;
        }
    }

    Constructor<?> con = cls.getConstructor(confClass);

    return instantiate(con, metaData);
}

From source file:org.apache.hawq.pxf.api.utilities.Utilities.java

/**
 * Creates an object using the class name. The class name has to be a class
 * located in the webapp's CLASSPATH./*from   w  w  w .  j  ava2s  .  c  om*/
 *
 * @param confClass the class of the metaData used to initialize the
 *            instance
 * @param className a class name to be initialized.
 * @param metaData input data used to initialize the class
 * @return Initialized instance of given className
 * @throws Exception throws exception if classname was not found in
 *             classpath, didn't have expected constructor or failed to be
 *             instantiated
 */
public static Object createAnyInstance(Class<?> confClass, String className, InputData metaData)
        throws Exception {

    Class<?> cls = null;
    try {
        cls = Class.forName(className);
    } catch (ClassNotFoundException e) {
        /* In case the class name uses the older and unsupported  "com.pivotal.pxf"
         * package name, recommend using the new package "org.apache.hawq.pxf"
         */
        if (className.startsWith("com.pivotal.pxf")) {
            throw new Exception("Class " + className + " does not appear in classpath. "
                    + "Plugins provided by PXF must start with \"org.apache.hawq.pxf\"", e.getCause());
        } else {
            throw e;
        }
    }

    Constructor<?> con = cls.getConstructor(confClass);

    return instantiate(con, metaData);
}

From source file:org.apache.hadoop.hive.ql.metadata.JarUtils.java

/**
 * Invoke 'getJar' on a JarFinder implementation. Useful for some job configuration contexts
 * (HBASE-8140) and also for testing on MRv2. First check if we have HADOOP-9426. Lacking that,
 * fall back to the backport.//ww w .  jav  a 2  s . c om
 *
 * @param my_class
 *          the class to find.
 * @return a jar file that contains the class, or null.
 */
private static String getJar(Class<?> my_class) {
    String ret = null;
    String hadoopJarFinder = "org.apache.hadoop.util.JarFinder";
    Class<?> jarFinder = null;
    try {
        log.debug("Looking for: {}", hadoopJarFinder);
        jarFinder = JavaUtils.loadClass(hadoopJarFinder);
        log.debug("Found: {}", hadoopJarFinder);
        Method getJar = jarFinder.getMethod("getJar", Class.class);
        ret = (String) getJar.invoke(null, my_class);
    } catch (ClassNotFoundException e) {
        log.debug("Using backported JarFinder.");
        ret = jarFinderGetJar(my_class);
    } catch (InvocationTargetException e) {
        // function was properly called, but threw it's own exception.
        // Unwrap it
        // and pass it on.
        throw new RuntimeException(e.getCause());
    } catch (Exception e) {
        // toss all other exceptions, related to reflection failure
        throw new RuntimeException("getJar invocation failed.", e);
    }

    return ret;
}

From source file:fm.last.hadoop.tools.ReplicationPolicyFixer.java

public static int verifyBlockPlacement(LocatedBlock lBlk, short replication, NetworkTopology cluster) {
    try {/* w ww  .j  a  v  a2  s.  com*/
        Class<?> replicationTargetChooserClass = Class
                .forName("org.apache.hadoop.hdfs.server.namenode.ReplicationTargetChooser");
        Method verifyBlockPlacementMethod = replicationTargetChooserClass.getDeclaredMethod(
                "verifyBlockPlacement", LocatedBlock.class, Short.TYPE, NetworkTopology.class);
        verifyBlockPlacementMethod.setAccessible(true);

        return (Integer) verifyBlockPlacementMethod.invoke(null, lBlk, new Short(replication), cluster);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            cause.printStackTrace();
            throw (RuntimeException) cause;
        } else {
            throw new RuntimeException(e);
        }
    }
}

From source file:ee.ria.xroad.common.conf.globalconf.IdentifierDecoderHelper.java

static ClientId getSubjectName(X509Certificate cert, IdentifierDecoderType decoder, String instanceIdentifier)
        throws Exception {
    String methodName = StringUtils.trim(decoder.getMethodName());
    Method method;//from   w  w w .  j  av a  2 s. c o  m
    try {
        method = getMethodFromClassName(methodName, X509Certificate.class);
    } catch (ClassNotFoundException e) {
        throw new CodedException(X_INTERNAL_ERROR, "Could not find identifier decoder: '%s'", methodName);
    } catch (NoSuchMethodException e) {
        throw new CodedException(X_INTERNAL_ERROR, "Could not find identifier decoder method: '%s'",
                methodName);
    } catch (Exception e) {
        throw new CodedException(X_INTERNAL_ERROR, e);
    }

    Object result;
    try {
        result = method.invoke(null /* Static, no instance */, cert);
    } catch (Exception e) {
        Throwable t = e instanceof InvocationTargetException ? e.getCause() : e;
        log.error("Error during extraction of subject name from " + "certificate '" + cert.getSubjectDN()
                + "' using " + "identifier decoder '" + methodName + "'", t);
        throw new CodedException(X_INCORRECT_CERTIFICATE, t);
    }

    if (result == null) {
        throw new CodedException(X_INCORRECT_CERTIFICATE,
                "Could not get SubjectName from certificate " + cert.getSubjectDN());
    }

    if (result instanceof String) {
        return ClientId.create(instanceIdentifier, decoder.getMemberClass(), (String) result);
    } else if (result instanceof String[]) {
        String[] parts = (String[]) result;
        if (parts.length == 2) {
            return ClientId.create(instanceIdentifier, parts[0], parts[1]);
        }
    } else if (result instanceof ClientId) {
        return (ClientId) result;
    }

    throw new CodedException(X_INTERNAL_ERROR,
            "Unexpected result from identifier decoder: " + result.getClass());
}