classloader example

This commit is contained in:
Raksha Rao 2018-02-05 11:09:34 +05:30
parent f12497c18b
commit 2f31b4a8cc
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package com.baeldung.classloader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class SampleClassLoader {
public void loadClass() throws ClassNotFoundException {
System.out.println("Classloader of this class:"+SampleClassLoader.class.getClassLoader());
Class.forName("com.baeldung.classloader.SampleClassLoader", true
, SampleClassLoader.class.getClassLoader().getParent());
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.classloader;
import org.junit.Test;
import static org.junit.Assert.*;
public class SampleClassLoaderTest {
@Test(expected = ClassNotFoundException.class)
public void givenAppClassLoader_whenParentClassLoader_thenClassNotFoundException() throws Exception {
SampleClassLoader sampleClassLoader = (SampleClassLoader) Class.forName(SampleClassLoader.class.getName()).newInstance();
sampleClassLoader.loadClass();
}
}