peterk
peterk

Reputation: 5444

Is it possible to select a classpath jar on application startup?

ie:

main_or_init()
{
    if(some platform specific condition) {
        addToClassPath("jarFileA.jar");
    } else {
        addToClassPath("JarFileB.jar");
    }

    // which implementation selected above 
    ClassFromJarFile firstInstance = new ClassFromJarFile();
}

Note: yes other ways of loading, factories etc. But if this can be done, then an entire platform specific package implementation might be dynamically selected at application startup.

Upvotes: 1

Views: 83

Answers (1)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81704

Remember that once a class is loaded it uses its own ClassLoader to load the other classes it needs. So all you need to do is have the first class in your application figure out the dynamic classpath and construct a URLClassLoader with the list of entries that you want. Then load the "real" main class using that ClassLoader, and you're done: the dynamic classpath will be used for the rest of the application -- or for any classes loaded via the "real" main class, in any event.

Upvotes: 4

Related Questions