List of usage examples for java.lang ClassLoader loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
From source file:ReflectionHelper.java
public static Class<?> classForName(String name, Class<?> caller) throws ClassNotFoundException { try {/*from w w w. j a va 2s . c o m*/ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); if (contextClassLoader != null) { return contextClassLoader.loadClass(name); } } catch (Throwable e) { // ignore } return Class.forName(name, true, caller.getClassLoader()); }
From source file:Main.java
private static Class<?> loadClassViaContext(String fqcn) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try {/*from w w w. j av a 2 s .com*/ return loader == null ? null : loader.loadClass(fqcn); } catch (ClassNotFoundException e) { } return null; }
From source file:com.gargoylesoftware.htmlunit.gae.GAETestRunner.java
private static Class<?> getClassFromGAELikeClassLoader(final Class<?> klass) { final ClassLoader gaeLikeLoader = new GAELikeClassLoader(); try {/*ww w .j a v a 2 s .c om*/ return gaeLikeLoader.loadClass(klass.getName()); } catch (final ClassNotFoundException e) { throw new RuntimeException( "Can't find existing test class through GAELikeClassLoader: " + klass.getName()); } }
From source file:Main.java
private static void loadProviderIfNecessary(String providerClassName) { if (providerClassName == null) { return;//from w ww . ja v a2 s . com } final Class<?> klass; try { final ClassLoader sysLoader = ClassLoader.getSystemClassLoader(); if (sysLoader != null) { klass = sysLoader.loadClass(providerClassName); } else { klass = Class.forName(providerClassName); } } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); return; } Constructor<?> constructor = null; for (Constructor<?> c : klass.getConstructors()) { if (c.getParameterTypes().length == 0) { constructor = c; break; } } if (constructor == null) { System.err.println("No zero-arg constructor found for " + providerClassName); System.exit(1); return; } final Object o; try { o = constructor.newInstance(); } catch (Exception e) { e.printStackTrace(); System.exit(1); return; } if (!(o instanceof Provider)) { System.err.println("Not a Provider class: " + providerClassName); System.exit(1); } Security.insertProviderAt((Provider) o, 1); }
From source file:com.germinus.easyconf.ClasspathUtil.java
/** * Return the Class object of the specified class name by searching the * current classpath and the system classpath. * * @param name the name of the class/*from ww w.ja v a 2 s . com*/ * * @return the <code>Class</code> instance */ public static Class locateClass(String name) throws ClassNotFoundException { Class foundClass = null; if (foundClass == null) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { foundClass = loader.loadClass(name); log.debug("Class loaded from the context classpath (" + name + ")"); } catch (ClassNotFoundException ignore) { } } if (foundClass == null) { try { foundClass = ClassLoader.getSystemClassLoader().loadClass(name); log.debug("Class loaded from the system classpath (" + name + ")"); } catch (ClassNotFoundException ignore) { } } if (foundClass == null) { throw new ClassNotFoundException( "Class " + name + " was not found " + "in context classpath nor system classpath"); } return foundClass; }
From source file:Main.java
private static Class<?> loadClassViaClasses(String fqcn, Class<?>[] classes) { if (classes != null) { for (Class<?> c : classes) { ClassLoader loader = c.getClassLoader(); if (loader != null) { try { return loader.loadClass(fqcn); } catch (ClassNotFoundException e) { // move on to try the next class loader }//ww w. j av a 2 s . c o m } } } return null; }
From source file:Main.java
/** * Tries to load the class from the preferred loader. If not successful, tries to * load the class from the current thread's context class loader or system class loader. * @param classname Desired class name.// w ww .ja v a2 s. co m * @param preferredLoader The preferred class loader * @return the loaded class. * @throws ClassNotFoundException if the class could not be loaded by any loader */ public static Class<?> loadClass(String classname, ClassLoader preferredLoader) throws ClassNotFoundException { ClassNotFoundException exception = null; for (ClassLoader loader : Arrays.asList(preferredLoader, Thread.currentThread().getContextClassLoader(), ClassLoader.getSystemClassLoader())) { try { return loader.loadClass(classname); } catch (ClassNotFoundException e) { if (exception == null) { exception = e; } } } throw exception; }
From source file:net.sf.nmedit.nomad.core.jpf.JPFServiceInstallerTool.java
@SuppressWarnings("unchecked") private static Class<Service> lookupServiceImplementationClass(Class<? extends Service> serviceClass, String serviceImplementationName, ClassLoader loader) throws ClassNotFoundException { Class<?> _class = loader.loadClass(serviceImplementationName); if (!serviceClass.isAssignableFrom(_class)) throw new ClassCastException("Service class is not subclass of " + serviceClass + ": " + _class); Class<Service> serviceImplementationClass = (Class<Service>) _class; return serviceImplementationClass; }
From source file:org.jsonschema2pojo.integration.ref.FragmentRefIT.java
@BeforeClass public static void generateAndCompileEnum() throws ClassNotFoundException { ClassLoader fragmentRefsClassLoader = classSchemaRule.generateAndCompile("/schema/ref/fragmentRefs.json", "com.example"); fragmentRefsClass = fragmentRefsClassLoader.loadClass("com.example.FragmentRefs"); }
From source file:com.bluecloud.ioc.classloader.ClassHandler.java
/** * <h3>Class</h3> ?classNameClass * // w w w.j av a 2 s . co m * @param className * ?Class?? * @param isNewInstance * ? * @return ?ClassLoader?ClassLoader?? * ClassNotFoundException * ???isNewInstancetrueObjectfalseClass<? extends Object> * @throws InstantiationException * @throws IllegalAccessException * @throws ClassNotFoundException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws SecurityException * @throws IllegalArgumentException */ public static Object getClass(String className, boolean isNewInstance) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { ClassLoader classloader = Thread.currentThread().getContextClassLoader(); Class<?> clazz = classloader.loadClass(className); if (isNewInstance) { return getClassNewInstrance(clazz); } return clazz; }