Java 8 URLClassLoader - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 URLClassLoader class.

This code example shows how to use the following methods:
    public ChildFirstClassLoadingTest() { 
    } 
 
    @Test 
    public void testWithoutExclusion() throws Exception { 
        URLClassLoader parent = new URLClassLoader(new URL[]{ChildFirstClassLoadingTest.class.getResource("parent.jar")}, this.getClass().getClassLoader());
        parent.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
        ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(), null, new URL[]{ChildFirstClassLoadingTest.class.getResource("child.jar")});
        Class<?> welcomeParent = child.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
        Class<?> welcomeChild = child.loadClass("org.jboss.as.model.test.child.WelcomeChild");
        Class<?> welcome = this.getClass().getClassLoader().loadClass("org.jboss.as.model.test.api.Welcome");
        welcomeChild.asSubclass(welcome);
        welcomeParent.asSubclass(welcome);
    } 
 
    @Test(expected = NoClassDefFoundError.class) 
    public void testWithExclusion() throws Exception { 
        URLClassLoader parent = new URLClassLoader(new URL[]{ChildFirstClassLoadingTest.class.getResource("parent.jar")}, this.getClass().getClassLoader());
        parent.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
        ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(),
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    ClassLoader cl = TestClassWithNoPackage.class.getClassLoader();
    String JAR = JarFinder.getJar(cl.loadClass(NAME));
 
    // Add testjob jar file to classpath. 
    Configuration conf = new Configuration();
    conf.setClassLoader(new URLClassLoader(new URL[]{new URL("file", null, JAR)}, 
                                           null)); 
    // Get class with no package name. 
    String defaultPackage = this.getClass().getPackage().getName();
    Class c = StreamUtil.goodClassOrNull(conf, NAME, defaultPackage);
    assertNotNull("Class " + NAME + " not found!", c);
  } 
  public static void main(String[]args) throws Exception
  { 
    new TestClassWithNoPackage().testGoodClassOrNull(); 
  } 
 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
			ClassLoaderPool.getClassLoader(StringPool.BLANK)); 
	} 
 
	@Test
	public void testGetClassLoaderWithValidContextName() { 
		ClassLoader classLoader = new URLClassLoader(new URL[0]);
 
		ClassLoaderPool.register(_CONTEXT_NAME, classLoader);
 
		Assert.assertSame(
			classLoader, ClassLoaderPool.getClassLoader(_CONTEXT_NAME));
	} 
 
	@Test
	public void testGetContextNameWithInvalidClassLoader() { 
		ClassLoader classLoader = new URLClassLoader(new URL[0]);
 
		ClassLoaderPool.register(_CONTEXT_NAME, classLoader);
 
		Assert.assertEquals(
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:loadClass
        sb.append("file:");
        sb.append(System.getProperty("test.src", "."));
        sb.append(File.separatorChar);
        sb.append(jarName);
 
        ClassLoader newLoader = new URLClassLoader(new URL[] { new URL(sb.toString()) });
        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
 
        Thread.currentThread().setContextClassLoader(newLoader);
        test(newLoader.loadClass(className));
        Thread.currentThread().setContextClassLoader(oldLoader);
 
        return newLoader;
    } 
 
    private static void test(Class type) throws Exception {
        Introspector.getBeanInfo(type);
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getResource, getResourceAsStream, loadClass
                .addResource("foo.txt", "Some text") 
                .addPluginInformation("someKey", "someName", "1.33") 
                .build(); 
        assertNotNull(jar);
 
        URLClassLoader cl = new URLClassLoader(new URL[]{jar.toURI().toURL()}, null);
        Class cls = cl.loadClass("my.Foo");
        assertNotNull(cls);
        Object foo = cls.newInstance();
        String result = (String) cls.getMethod("hi").invoke(foo);
        assertEquals("hi", result);
        Assert.assertEquals("Some text", IOUtils.toString(cl.getResourceAsStream("foo.txt")));
        assertNotNull(cl.getResource("META-INF/MANIFEST.MF"));
 
        String xml = IOUtils.toString(cl.getResourceAsStream("maera-plugin.xml"));
        assertTrue(xml.indexOf("someKey") > 0);
        assertTrue(xml.indexOf("someName") > 0);
        assertTrue(xml.indexOf("1.33") > 0);
    } 
} 
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:loadClass
  } 
   
  private void run(String exclusionsFile) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    String shrikeBin = new File("../com.ibm.wala.shrike/bin/").getCanonicalPath();
    String utilBin = new File("../com.ibm.wala.util/bin/").getCanonicalPath();
    URLClassLoader jcl = new URLClassLoader(new URL[]{ new URL("file:///tmp/test.jar"), new URL("file://" + shrikeBin + "/"), new URL("file://" + utilBin + "/") }, DynamicCallGraphTests.class.getClassLoader().getParent());
  
    Class<?> testClass = jcl.loadClass("dynamicCG.MainClass");
    Assert.assertNotNull(testClass);
    Method testMain = testClass.getDeclaredMethod("main", String[].class);
    Assert.assertNotNull(testMain);
 
    System.setProperty("dynamicCGFile", "/tmp/cg.txt");
    if (exclusionsFile != null) {
      File tmpFile = TemporaryFile.urlToFile("exclusions.txt", getClass().getClassLoader().getResource(exclusionsFile));
      System.setProperty("dynamicCGFilter", tmpFile.getCanonicalPath());
    } 
    try { 
      testMain.invoke(null, (Object)new String[0]);      
    } catch (Throwable e) {

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:loadClass
 
	public static void main(String[] args) throws Exception {
//		ClassLoader cl = new TracingClassLoader(new File("bin")); 
		 
		URLClassLoader cl = createGarbageCollectableClassLoader(new File("bin"));
		Class<?> clazz = cl.loadClass("spikes.klaus.classloading.HelloWorld");
		clazz.newInstance();
		 
		cl = null;
		clazz = null;
		System.gc();
		checkIfResourcesAreFreed(); 
	} 
 
	private static void checkIfResourcesAreFreed() throws IOException { 
		System.out.println("File deleted: " + new File("tmp.tmp").delete());
		new ServerSocket(12345).close();
	} 
 
	private static URLClassLoader createGarbageCollectableClassLoader(File binDirectory) throws Exception {

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    } 
 
    @Test
    public void testNonDefaultClassLoader() throws Exception {
 
        ClassLoader cl = new URLClassLoader(new URL[0],
                Thread.currentThread().getContextClassLoader());
 
        Thread.currentThread().setContextClassLoader(cl);
 
        Tomcat tomcat = getTomcatInstance();
        tomcat.getServer().setParentClassLoader(cl);
 
        // Must have a real docBase - just use temp 
        Context ctx = 
            tomcat.addContext("", System.getProperty("java.io.tmpdir"));
 
        Tomcat.addServlet(ctx, "ClassLoaderReport", new ClassLoaderReport(cl));
        ctx.addServletMapping("/", "ClassLoaderReport"); 
 

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
        super(new URLClassLoader(new URL[]{
                //integrationClassesDir.toURL(), 
                //integrationTestClassesDir.toURL(), 
                //new File(srcDir, "test/java").toURL(), 
                new File(TestContext.getInstance().findIntegrationTestProjectRoot(), "src/test/resources/packaged-changelog.jar").toURL(),
                new File(System.getProperty("java.io.tmpdir")).toURL(),
        })); 
 
    } 
} 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getParent, loadClass
				getOutputDir(), file, TestHelper.visitAllFiles( getOutputDir(), list ), "1.5",
				TestHelper.buildClasspath( jars )
		); 
		URL[] urls = new URL[]{file.toURL()};
		Thread currentThread = Thread.currentThread();
		URLClassLoader ucl = new URLClassLoader( urls, currentThread.getContextClassLoader() );
		currentThread.setContextClassLoader( ucl );
 
		AnnotationConfiguration configuration = new AnnotationConfiguration();
		Class train = ucl.loadClass( "org.hibernate.tool.hbm2x.Train" );
		Class passenger = ucl.loadClass( "org.hibernate.tool.hbm2x.Passenger" );
		Class transportationPk = ucl.loadClass( "org.hibernate.tool.hbm2x.TransportationPk" );
		configuration.addAnnotatedClass( train );
		configuration.addAnnotatedClass( passenger );
 
		configuration.setProperty( "hibernate.hbm2ddl.auto", "create-drop" );
		SessionFactory sf = configuration.buildSessionFactory();
		Session s = sf.openSession();
 
		Object trainId = transportationPk.newInstance();

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 URLClassLoader Questions & Answers:

See Code Examples for Java 8 URLClassLoader Methods: