In this post we will give you a simple example of custom class loader. We will also touch upon out of the box class loaders in java and the chaining concept.
Java Class Loaders
- Bootstrap Class Loader
- Extensions Class Loader
- System Class Loader
- Custom class loader or User Defined Class Loader
Class loader responsibility
Name | Class loader class | Responsible to load | Level |
---|---|---|---|
Bootstrap | Written in native language | JAVA_HOME/lib | 1 |
Extension | sun.misc.Launcher$ExtClassLoader | JAVA_HOME/lib/ext | 2 |
System | sun.misc.Launcher$AppClassLoader | Classpath | 3 |
Custom | Your Class | Custom Location | 4 |
Except the bootstrap class loader every other class loader is written in java. Every class loader except the bootstrap has a parent class loader. When a class loader is asked to load a class, it request it’s parent to load the class. Only if the parent is not able to find the class then the class loader itself will try to find the class. This process is called chaining.
Steps to write a custom class loader
- Extend java.lang.ClassLoader.
- Override public Class<?> loadClass(final String name) method.
- Load class file as java.io.File or resource stream.
- Convert the file stream to byte array.
- Call Class<?> defineClass(String name, byte[] b, int off, int len) method in the java.lang.ClassLoader class.
- Call void resolveClass(Class<?> c) in the java.lang.ClassLoader class.
Custom Class Loader Code Snippet
1 2 3 4 5 6 7 8 9 10 11 |
public class CustomClassLoader extends ClassLoader { @Override public Class<?> loadClass(final String name) { final InputStream inputStream = new FileInputStream(new File(name)); byte [] data = new byte[inputStream.available()]; inputStream.read(data); Class<?> clazz = defineClass(name, data, 0, data.length); resolveClass(clazz); return clazz; } } |
Class Loader Static Design
Complete Sample Code
CustomClassLoader.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package com.ourownjava.corejava.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; /** * This class loader shall load classes from a custom location which is not part of the class path. * * In real time scenario this location can be a network file system, or a socket or an encrypted class. * * @author ourownjava.com * */ public class CustomClassLoader extends ClassLoader { private static final String CLASS_FILE_LOCATION = "/home/thosan/java/clazzez/"; /** * Override the loadClass method and delegate the responsibility to load the * class to it's super class. If the parent class loader is not able to load * the class then this class loader shall try to load the class by itself. * */ @Override public Class<?> loadClass(final String name) { Class<?> clazz = null; try { clazz = super.loadClass(name); } catch (final ClassNotFoundException e) { //This means the parent class is not able to find the class. //not in <JAVA_HOME>/lib, <JAVA_HOME>/lib/ext, -cp, -classpath //try to load the class using our own custom class loader e.printStackTrace(); } if(null == clazz){ try { final byte [] clazzByte = loadClassByte(name); clazz = defineClass(name, clazzByte, 0, clazzByte.length); resolveClass(clazz); } catch (final IOException e) { //this means the given class is not found the custom location e.printStackTrace(); } } return clazz; } /** * This method shall take the class name and load the class file. * * @param name * @return * @throws IOException */ private byte[] loadClassByte(final String name) throws IOException { // convert the give class name to file format like // /home/thosan/java/clazzez/sanju/org/corejava/util/ClassDoesNotExistInClassPath final StringBuilder fileNameBuilder = new StringBuilder(CLASS_FILE_LOCATION); fileNameBuilder.append(name.replace('.', File.separatorChar)); fileNameBuilder.append(".class"); //read the file and grab the byte stream byte[] data = null; InputStream inputStream = null; try { inputStream = new FileInputStream(new File(fileNameBuilder.toString())); data = new byte[inputStream.available()]; inputStream.read(data); } catch (FileNotFoundException e) { e.printStackTrace(); throw new FileNotFoundException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new IOException(e.getMessage(), e); }finally{ if(null != inputStream){ inputStream.close(); } } return data; } } |
ClassToLoad.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.ourownjava.corejava.util; /** * * @author ourownjava.com * */ public class ClassToLoad { public void aMethod(){ System.out.println("ClassToLoad.aMethod()"); } } |
ClassDoesNotExistInClassPath.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.sanju.corejava.util; /** * * @author ourownjava.com * */ public class ClassDoesNotExistInClassPath { public void aMethod(){ System.out.println("ClassDoesNotExistInClassPath.aMethod()"); } } |
TestCustomClassLoader.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.ourownjava.corejava.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import org.junit.Before; import org.junit.Test; /** * * @author ourownjava.com * */ public class TestCustomClassLoader { private CustomClassLoader customClassLoader; @Before public void setUp(){ customClassLoader = new CustomClassLoader(); } /** * com.ourownjava.corejava.util.ClassToLoad is in class path so the system class loader should load it. * * @throws ClassNotFoundException */ @Test public void shoudlLoadClassUsingSystemClassLoader() throws ClassNotFoundException{ final Class<?> clazz = customClassLoader.loadClass("com.ourownjava.corejava.util.ClassToLoad"); assertNotNull(clazz); assertEquals("sun.misc.Launcher$AppClassLoader", clazz.getClassLoader().getClass().getName()); } /** * org.sanju.corejava.util.ClassDoesNotExistInClassPath is not in class path, so the custom class loader should load it. * * @throws ClassNotFoundException */ @Test public void shouldLoadClassUsingCustomClassLoader() throws ClassNotFoundException{ final Class<?> clazz = customClassLoader.loadClass("org.sanju.corejava.util.ClassDoesNotExistInClassPath"); assertNotNull(clazz); assertEquals("com.ourownjava.corejava.util.CustomClassLoader", clazz.getClassLoader().getClass().getName()); } } |
https://peoplesketo-gummies.com KETO diet pharmacy People's KETO pharmacy. Ремонт квартир этапы ремонта.