List of usage examples for java.net URLClassLoader loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
From source file:fi.jumi.actors.maven.GenerateEventStubsMojo.java
private static Class<?> loadFromClasspath(String className, File[] classpath) throws ClassNotFoundException { try {/*from w w w . j a v a 2s . c o m*/ URL[] urls = FileUtils.toURLs(classpath); URLClassLoader loader = new URLClassLoader(urls); return loader.loadClass(className); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.superfuntime.chatty.Core.java
@SuppressWarnings("unchecked") private static void startPlugins() { logger.info("Loading available plugins"); final Collection<File> botJars = FileUtils.listFiles(pluginsDirecotry, new String[] { "jar" }, false); for (File botJar : botJars) { try {// ww w. java2 s .c o m JarFile jar = new JarFile(botJar); InputStream is = jar.getInputStream(jar.getEntry("plugin.yml")); YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true); String mainClass = n.getString("mainClass"); String name = n.getString("name"); URLClassLoader loader = new URLClassLoader(new URL[] { botJar.toURI().toURL() }, Core.class.getClassLoader()); Plugin plugin = loader.loadClass(mainClass).asSubclass(Plugin.class).newInstance(); plugin.start(); PLUGIN_INFO.put(name, new PluginInfo(name, plugin, n)); logger.info("Loaded plugin '{}'", name); } catch (Exception e) { logger.error("Failed to load plugin from {}", botJar.getName()); e.printStackTrace(); } } logger.info("Loaded {} plugin(s)", PLUGIN_INFO.size()); }
From source file:org.superfuntime.chatty.Core.java
@SuppressWarnings("unchecked") private static void loadBots() { logger.info("Loading available bots"); final Collection<File> botJars = FileUtils.listFiles(botsDirecotry, new String[] { "jar" }, false); for (File botJar : botJars) { try {// w w w .j a va2 s . c o m JarFile jar = new JarFile(botJar); InputStream is = jar.getInputStream(jar.getEntry("bot.yml")); YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true); String mainClass = n.getString("mainClass"); String type = n.getString("type"); URLClassLoader loader = new URLClassLoader(new URL[] { botJar.toURI().toURL() }, Core.class.getClassLoader()); BOT_INFO.put(type, new BotClassInfo(loader.loadClass(mainClass).asSubclass(Bot.class), n)); logger.info("Loaded bot '{}'", type); } catch (Exception e) { logger.error("Failed to load bot from {}", botJar.getName()); e.printStackTrace(); } } logger.info("Loaded {} bot(s)", BOT_INFO.size()); }
From source file:org.cmg.ml.sam.SamProjectHelper.java
public static Class<?> loadClassFromProject(ClassLoader parentClassLoader, Resource resource, IProject project) throws CoreException, MalformedURLException, ClassNotFoundException { String[] classPathEntries = JavaRuntime.computeDefaultRuntimeClassPath(JavaCore.create(project)); // URL url = JavaCore.create(project).getOutputLocation().toFile(). // List<URL> urlList = new ArrayList<URL>(); // for (int i = 0; i < classPathEntries.length; i++) { String entry = classPathEntries[0]; IPath path = new Path(entry); URL url = path.toFile().toURI().toURL(); // urlList.add(url); // }/*from www. j ava2s.co m*/ // ClassLoader parentClassLoader = project.getClass().getClassLoader(); // URL[] urls = (URL[]) urlList.toArray(new URL[urlList.size()]); // URLClassLoader classLoader = new URLClassLoader(urls, cLoader); URLClassLoader classLoader = new URLClassLoader(new URL[] { url }, parentClassLoader); Class<?> clazz = classLoader.loadClass(SamProjectHelper.getPackageFolder(resource).replace('/', '.') + "." + SamProjectHelper.getClassName(resource)); return clazz; }
From source file:org.panthercode.arctic.core.reflect.ReflectionUtils.java
/** * Creates a list of all classes from a specific jar file. * * @param path filepath to jar file//from w w w . j a v a2 s . com * @return Returns a list with all classes in jar file. If jar file doesn't contain any class, the list will be empty. * @throws IOException Is thrown if an error occurs while reading the .jar file. * @throws ClassNotFoundException Is thrown if the class is not found. * @throws IllegalArgumentException Is thrown if path is null. */ public static List<Class<?>> extractClassesFromJar(String path) throws IOException, ClassNotFoundException, IllegalArgumentException { ArgumentUtils.assertNotNull(path, "path"); List<Class<?>> classList = new ArrayList<>(); URL[] urlArray = { new URL("jar:file:" + path + "!/") }; URLClassLoader classLoader = new URLClassLoader(urlArray); for (String className : ReflectionUtils.extractClassNamesFromJar(path)) { classList.add(classLoader.loadClass(className)); } return classList; }
From source file:org.roda.core.util.ClassLoaderUtility.java
/** * Adds the given {@link URL}s to the classpath and creates an instance of the * given class.//from w ww . j a v a 2s . c o m * * @param urls * a list of {@link URL}s to be added to the classpath. * @param className * the name of the class for which to create an instance. * @return the instance created. * * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static Object createObject(URL[] urls, String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException { if (StringUtils.isBlank(className)) { throw new IllegalArgumentException("className cannot be null"); } URLClassLoader clazzLoader = new URLClassLoader(urls, CLASS_LOADER); LOGGER.trace("Loading class {} with ClassLoader {}", className, CLASS_LOADER.getClass().getSimpleName()); return clazzLoader.loadClass(className).newInstance(); }
From source file:org.roda.core.util.ClassLoaderUtility.java
/** * Adds the given file path to the classpath and creates an instance of the * given class name.//w w w. j av a 2 s . c o m * * @param filePath * the path of the file to add to the classpath. * @param className * the name of the class for which to create an instance. * @return the instance created. * * @throws IOException * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static Object createObject(String filePath, String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { if (StringUtils.isBlank(className)) { throw new IllegalArgumentException("className cannot be null"); } ClassLoaderUtility.addFile(filePath); String path = "jar:file://" + filePath + "!/"; URL url = new File(path).toURL(); URLClassLoader clazzLoader = new URLClassLoader(new URL[] { url }, CLASS_LOADER); LOGGER.trace("Loading class {} with ClassLoader {}", className, CLASS_LOADER.getClass().getSimpleName()); return clazzLoader.loadClass(className).newInstance(); }
From source file:com.googlecode.dex2jar.test.TestUtils.java
public static File dex(List<File> files, File distFile) throws Exception { String dxJar = "src/test/resources/dx.jar"; File dxFile = new File(dxJar); if (!dxFile.exists()) { throw new RuntimeException("dx.jar?"); }//from w ww.ja v a 2s . com URLClassLoader cl = new URLClassLoader(new URL[] { dxFile.toURI().toURL() }); Class<?> c = cl.loadClass("com.android.dx.command.Main"); Method m = c.getMethod("main", String[].class); if (distFile == null) { distFile = File.createTempFile("dex", ".dex"); } List<String> args = new ArrayList<String>(); args.addAll(Arrays.asList("--dex", "--no-strict", "--output=" + distFile.getCanonicalPath())); for (File f : files) { args.add(f.getCanonicalPath()); } m.invoke(null, new Object[] { args.toArray(new String[0]) }); return distFile; }
From source file:com.expedia.tesla.compiler.Util.java
/** * Load a Java class by it's full name.//from www . j ava 2 s . c o m * * @param name * The full name of the Java class to load. * @param urls * An array of URL objects represent class paths or jar files. * @return The java.lang.Class object represents the Java class. * @throws ClassNotFoundException * If the class is not found. * @throws IOException * If the jar file cannot open. */ public static java.lang.Class<?> loadClass(String name, URL[] urls) throws ClassNotFoundException, IOException { java.lang.Class<?> clzz = null; if (urls != null && urls.length > 0) { URLClassLoader cl = URLClassLoader.newInstance(urls, Util.class.getClassLoader()); clzz = cl.loadClass(name); } if (clzz == null) { clzz = Class.forName(name); } return clzz; }
From source file:io.fabric8.maven.core.util.MavenUtil.java
/** * Returns true if any of the given class names could be found on the given class loader *///from w w w .j a v a2 s .co m public static boolean hasClass(MavenProject project, String... classNames) { URLClassLoader compileClassLoader = getCompileClassLoader(project); for (String className : classNames) { try { compileClassLoader.loadClass(className); return true; } catch (Throwable e) { // ignore } } return false; }