Example usage for java.net URLClassLoader newInstance

List of usage examples for java.net URLClassLoader newInstance

Introduction

In this page you can find the example usage for java.net URLClassLoader newInstance.

Prototype

public static URLClassLoader newInstance(final URL[] urls) 

Source Link

Document

Creates a new instance of URLClassLoader for the specified URLs and default parent class loader.

Usage

From source file:com.datatorrent.stram.api.IotDev.java

public static URLClassLoader loadDependencies(LinkedHashSet<URL> launchDependencies) {
    URLClassLoader cl = URLClassLoader
            .newInstance(launchDependencies.toArray(new URL[launchDependencies.size()]));
    Thread.currentThread().setContextClassLoader(cl);
    StringCodecs.check();//from w w w. j  a v  a 2 s .  c  o  m
    return cl;
}

From source file:org.apache.taverna.wsdl.testutils.WSDLTestHelper.java

@BeforeClass
public static void makeClassLoader() throws MalformedURLException {
    if (classLoader == null) {
        URL[] urls = new URL[] { new URL(TEST_CASES_URL) };
        classLoader = URLClassLoader.newInstance(urls);
    }/*from   w ww .  j  ava2 s .  co  m*/

    Assume.assumeNotNull(getResource(README_MD));
}

From source file:org.oxenburgh.spritnesse.Utils.java

static public URLClassLoader createClassLoader(String fileName) {
    String urlPath = makeUrlPath(fileName);
    URL url = null;/*from ww  w.j  av a 2 s  .c  o  m*/
    try {
        url = new URL(urlPath);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
    URLClassLoader loader = URLClassLoader.newInstance(new URL[] { url });

    return loader;
}

From source file:org.huahinframework.manager.queue.RunQueue.java

@SuppressWarnings("unchecked")
@Override/*from w w  w. ja  va 2  s . com*/
public void run() {
    try {
        File jarFile = new File(queue.getJar());
        URL[] urls = { jarFile.toURI().toURL() };
        ClassLoader loader = URLClassLoader.newInstance(urls);
        Class<Tool> clazz = (Class<Tool>) loader.loadClass(queue.getClazz());

        if (!(clazz.newInstance() instanceof Tool)) {
            queue.setMessage("this jar not supported. this class dose not instance of Tool.class.");
            QueueUtils.registerQueue(queuePath, queue);
            return;
        }

        log.info("job start: " + clazz.getName());
        ToolRunner.run(jobConf, clazz.newInstance(), queue.getArguments());
        log.info("job end: " + clazz.getName());
    } catch (Exception e) {
        queue.setMessage(e.toString());
        try {
            QueueUtils.registerQueue(queuePath, queue);
        } catch (IOException e1) {
            e1.printStackTrace();
            log.error(e1);
        }
        e.printStackTrace();
        log.error(e);
    }
}

From source file:net.jselby.pc.bukkit.BukkitLoader.java

@SuppressWarnings("unchecked")
public void loadPlugin(File file)
        throws ZipException, IOException, InvalidPluginException, InvalidDescriptionException {
    if (file.getName().endsWith(".jar")) {
        JarFile jarFile = new JarFile(file);

        URL[] urls = { new URL("jar:file:" + file + "!/") };
        URLClassLoader cl = URLClassLoader.newInstance(urls);

        ZipEntry bukkit = jarFile.getEntry("plugin.yml");
        String bukkitClass = "";
        PluginDescriptionFile reader = null;
        if (bukkit != null) {
            reader = new PluginDescriptionFile(new InputStreamReader(jarFile.getInputStream(bukkit)));
            bukkitClass = reader.getMain();
            System.out.println("Loading plugin " + reader.getName() + "...");
        }//from   w  w  w .  j  av  a2s . c  o m

        jarFile.close();

        try {
            //addToClasspath(file);
            JavaPluginLoader pluginLoader = new JavaPluginLoader(PoweredCube.getInstance().getBukkitServer());
            Plugin plugin = pluginLoader.loadPlugin(file);
            Class<JavaPlugin> pluginClass = (Class<JavaPlugin>) plugin.getClass().getSuperclass();
            for (Method method : pluginClass.getDeclaredMethods()) {
                if (method.getName().equalsIgnoreCase("init")
                        || method.getName().equalsIgnoreCase("initialize")) {
                    method.setAccessible(true);
                    method.invoke(plugin, null, PoweredCube.getInstance().getBukkitServer(), reader,
                            new File("plugins" + File.separator + reader.getName()), file, cl);
                }
            }

            // Load the plugin, using its default methods
            BukkitPluginManager mgr = (BukkitPluginManager) Bukkit.getPluginManager();
            mgr.registerPlugin((JavaPlugin) plugin);
            plugin.onLoad();
            plugin.onEnable();
        } catch (Exception e1) {
            e1.printStackTrace();
        } catch (Error e1) {
            e1.printStackTrace();
        }

        jarFile.close();
    }
}

From source file:com.github.dirkraft.jash.ShExecutableJarBundler.java

@Override
public List<String> validateAndInit() throws Exception {
    List<String> errors = super.validateAndInit();

    if (!jarFile.isFile()) {
        errors.add(jarFile + " is not a file.");
        return errors;
    }/*  www.  j a  v  a2 s .c om*/

    JarFile jar = new JarFile(jarFile);
    Manifest manifest = jar.getManifest();
    String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
    ClassLoader jarClassLoader = URLClassLoader.newInstance(new URL[] { jarFile.toURI().toURL() });
    try {
        ClassUtils.getClass(jarClassLoader, mainClass, false);
    } catch (ClassNotFoundException e) {
        errors.add("Could not find Main-Class: " + mainClass + " in jar " + jarFile.getAbsolutePath());
        return errors;
    }

    _targetBinary = outputDir.toPath().resolve(commandName).toFile();
    if (_targetBinary.exists()) {
        errors.add("Target binary path " + _targetBinary.getAbsolutePath()
                + " is occupied. Will not overwrite, so aborting.");
    }

    return errors;
}

From source file:com.metadave.stow.TestStow.java

@Test
public void testGen() throws Exception {
    InputStream is = this.getClass().getResourceAsStream("/Stow.stg");
    File f = File.createTempFile("stow", "test.stg");
    String root = f.getParent();//from ww  w.  ja  va2  s.c o m
    String classdir = root + File.separator + "stowtest";

    String stgPath = f.getAbsolutePath();
    String stowStg = org.apache.commons.io.IOUtils.toString(is);

    org.apache.commons.io.FileUtils.writeStringToFile(f, stowStg);

    File d = new File(classdir);
    d.mkdirs();

    Stow.generateObjects(stgPath, "stowtest", "Test", classdir);

    File testSTBean = new File(classdir + File.separator + "TestSTBean.java");
    assertTrue(testSTBean.exists());

    File testSTAccessor = new File(classdir + File.separator + "TestSTAccessor.java");
    assertTrue(testSTAccessor.exists());

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    compiler.run(null, null, null, testSTBean.getAbsolutePath());
    compiler.run(null, null, null, testSTAccessor.getAbsolutePath());

    File testSTAccessorClass = new File(classdir + File.separator + "TestSTAccessor.class");
    assertTrue(testSTAccessorClass.exists());

    File testSTBeanClass = new File(classdir + File.separator + "TestSTBean.class");
    assertTrue(testSTBeanClass.exists());

    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File(root).toURI().toURL() });
    Class<?> cls = Class.forName("stowtest.TestSTBean", true, classLoader);

    Constructor<?> c = cls.getConstructors()[0];
    STGroup stg = new STGroupFile(f.getAbsolutePath());
    Object o = c.newInstance(stg);

    Set<String> methods = new HashSet<String>();
    methods.add("addPackage");
    methods.add("addBeanClass");
    methods.add("addTemplateName");
    methods.add("addAccessor");

    int count = 0;
    for (Method m : o.getClass().getMethods()) {
        if (methods.contains(m.getName())) {
            count++;
        }
    }
    assertEquals("Look for 8 generated methods", 8, count);
}

From source file:com.rk.grid.cluster.slave.NodeMain.java

/**
 * @return/*from   w  w w  .  j  a  v  a2s.co  m*/
 * @throws MalformedURLException 
 */
private static ClassLoader getClassLoader(String pathname) throws MalformedURLException {
    //      {
    //         URLClassLoader loader = new URLClassLoader(new URL[] { new URL("jar:file:/C:\\temp\\Caf1.jar!/") });
    //      }
    {
        File file = new File(pathname);

        if (!file.exists())
            throw new RuntimeException("File not find: " + pathname);

        URL[] urls = { file.toURI().toURL() };
        final URLClassLoader urlClassLoader = URLClassLoader.newInstance(urls);
        return urlClassLoader;
    }
}

From source file:org.adscale.testtodoc.TestToDoc.java

URLClassLoader createClassLoader(String fileName) throws Exception {
    String urlPath = makeUrlPath(fileName);
    URL url = new URL(urlPath);
    return URLClassLoader.newInstance(new URL[] { url });
}

From source file:bs.java

private static void loadable(String filename) {
    File file = new File(filename);

    JarInputStream is;/* w  w w. j  av a 2 s.  com*/
    try {
        ClassLoader loader = URLClassLoader.newInstance(new URL[] { file.toURI().toURL() });
        is = new JarInputStream(new FileInputStream(file));
        JarEntry entry;
        while ((entry = is.getNextJarEntry()) != null) {
            if (entry.getName().endsWith(".class") && !entry.getName().contains("/")) {
                Class<?> cls = Class.forName(FilenameUtils.removeExtension(entry.getName()), false, loader);
                for (Class<?> i : cls.getInterfaces()) {
                    if (i.equals(Loadable.class)) {
                        Loadable l = (Loadable) cls.newInstance();
                        Bs.addModule(l);
                    }
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}