Java Tips Weblog

  • Blog Stats

    • 1,207,483 hits
  • Categories

  • Archives

File Class Loader

Posted by Darryl Burke on December 9, 2008

Loading a Java class identified at runtime can be a daunting task. When faced with the challenge, one naturally turns first to the standard ClassLoader and its subclasses. However, many methods that look interesting are protected and/or final, and cannot be used directly.

FileClassLoader extends java.net.URLClassLoader to provide useful functionality for loading a class from its compiled class file.

Using FileClassLoader is not complicated, but some knowledge and understanding of reflection will come in handy for making use of the loaded class. The following lines of code will load a class and invoke its main method just as if run from a command prompt or an IDE. The variable file refers to a java.io.File representing a .class file. Exception handling is omitted for brevity.

FileClassLoader loader = new FileClassLoader();
Class clazz = loader.createClass(file);
Method method = clazz.getDeclaredMethod("main", String[].class);
method.invoke(clazz, (String) null);

If the class to be loaded has inner classes and/or requires access to other classes under the same package root, a few more lines are needed to add the package root to the class loader’s search path.

FileClassLoader loader = new FileClassLoader();
Class clazz = loader.createClass(file);
String className = clazz.getCanonicalName();
int levels = className.replaceAll("[^.]*", "").length();
File root = file.getParentFile();
for (int i = 0; i < levels; i++) {
root = root.getParentFile();
}
loader.addURL(root.toURI().toURL());
Method method = clazz.getDeclaredMethod("main", String[].class);
method.invoke(clazz, (String) null);

The addURL(…) method can be invoked in advance of loading a class from file, to add folders or jar files required to be available on its runtime classpath.

FileClassLoader can also load a class from a byte array, which facilitates loading a class across a network without the need to save the bytecode to a file.

Get The Code

FileClassLoader.java

See Also

The Java™ Tutorials: The Reflection API

Related Reading

Java API: java.net.URLClassLoader
Java API: java.lang.Class
Java API: java.lang.reflect.Method

About these ads

2 Responses to “File Class Loader”

  1. Hello.
    May I use your class “FileClassLoader.java” in my GPL’ed software (referring to your name and this URL)? You helped me a lot. Thanks in advance.

    Ralf Wohner

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: