3

I need to allow a user to specify the implementation of an interface at runtime via a config file, similar to in this question: Specify which implementation of Java interface to use in command line argument

However, my situation is different in that the implementations are not known at compile time, so I will have to use reflection to instantiate the class. My question is ... how do I structure my application such that my class can see the new implementation's .jar, so that it can load the class when I call:

Class.forName(fileObject.getClassName()).newInstance()

?

1
  • 1
    You don't need to do anything special. As long as the user put the jar file in the classpath, loading the class will work fine. Commented Aug 10, 2015 at 16:08

1 Answer 1

6

The comment is correct; as long as the .jar file is in your classpath, you can load the class.

I have used something like this in the past:

public static MyInterface loadMyInterface( String userClass ) throws Exception
{
    // Load the defined class by the user if it implements our interface
    if ( MyInterface.class.isAssignableFrom( Class.forName( userClass ) ) )
    {
        return (MyInterface) Class.forName( userClass ).newInstance();
    }
    throw new Exception("Class "+userClass+" does not implement "+MyInterface.class.getName() );
}

Where the String userClass was the user-defined classname from a config file.


EDIT

Come to think of it, it is even possible to load the class that the user specifies at runtime (for example, after uploading a new class) using something like this:

public static void addToClassPath(String jarFile) throws IOException 
{
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class loaderClass = URLClassLoader.class;

    try {
        Method method = loaderClass.getDeclaredMethod("addURL", new Class[]{URL.class});
        method.setAccessible(true);
        method.invoke(classLoader, new Object[]{ new File(jarFile).toURL() });
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IOException( t );
    }
}

I remember having found the addURL() invocation using reflection somewhere here on SO (of course).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.