URLClassLoader

Java.net.URLClassLoader Example

In this example, we shall see how we can use the java.net.URLClassLoader Class in the java.net package for loading the classes dynamically.

The URLClassLoader class Loads the classes and other resources at runtime from a directory or a JAR file. It has constructors that accept an array of URL class, URL[] and delegating parent Class-Loader, optionally.
 
 
 
 
 
 

1. URLClassLoader Constructors

  • URLClassLoader(URL[] urls)
  • URLClassLoader(URL[] urls, ClassLoader parent)
  • URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory)

We will create a basic PoJo and attempt to load it using the URLClassLoader class. We will invoke the foo() after successful loading.

JavaPoJo.java:

package com.jcg.examples;

public class JavaPoJo
{
		public void foo()
		{
				System.out.println("FooClass successfully loaded!");
		}
}

Now we compile this JavaPoJo.java File and package into a JAR file either by exporting it with jar command from the commandline or by exporting from Eclispe IDE.
Then we create an URL object and pass the path of that JAR file.

TIP:
It is mandatory to specify the protocol of the URL we are attempting to instatiate using the URL class like HTTP, File.. etc.

URLClassLoaderDemo.java:

package com.jcg.examples;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;


public class URLClassLoaderDemo
{
	public static void main(String[] args) throws MalformedURLException
	{
			List urls = new ArrayList();
			urls.add(new URL("file:\\C:\\Users\\CENTAUR\\Desktop\\jcg.jar"));

			try (URLClassLoader urlClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()])))
			{
					//Loading the class
					Class simpleClass = urlClassLoader.loadClass("com.jcg.examples.JavaPoJo");

					//accessing the constructor to instantiate the class
					Constructor simpleConstructor = simpleClass.getConstructor();

					//instantiating the class
					Object simpleClassObj = simpleConstructor.newInstance();

					//accessing an instance method
					Method method = simpleClass.getMethod("foo");

					//invoking the method..
					method.invoke(simpleClassObj);


			}
			catch (Exception e)
			{
					e.printStackTrace();
			}
	}
}

We create a List <URL> and convert that into an URL array and pass it into the URLClassLoader constructor. Once we have successfully created the Classloader, we load our JavaPoJo class, compiler earlier.
After loading we instantiate it using the newInstance() method of java.lang.reflect.Constructor class. Then we invoke the foo() method on that JavaPoJo instance.

Following output is rendered on successful execution of the URLClassLoaderDemo class:

OUTPUT:

FooClass successfully loaded!

There are a number of other useful methods ,too, provided by the UrlClassLoader class for the convenience of the user.

  • findClass(String name): Loads a class with the specified name.
  • addURL(URL url) Add the URL to the existing list of URLs to the ClassLoader.
  • public URL[] getURLs() Get the list of URLs of the URLClassLoader instance.

2. Conclusion

Here, we tried to understand how to load a class via URL and invoke a method using the reflection API

Download
You can download the source code of this example here: URLClassLoaderDemo.zip

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button