Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:16:21+00:00 2026-06-06T09:16:21+00:00

For stand-alone java application I can use the following codes to load jar lib

  • 0

For stand-alone java application I can use the following codes to load jar lib dynamically during the runtime based on the library path. It seems not working if I deploy same codes in Java Web container and run as a servlet. I actually want to be able to load the different jar libs based on the jar lib path in servlet request.

It means that single servlet will have to be able to load different jar libs dynamically during runtime and run onward biz logic

user1 might request to load jar files under /tmp/lib/v1.0/.jar
user2 might request to load jar files under /tmp/lib/v1.1/
.jar
(The jar files in v1.0 and v1.1 got the exactly same class names)

Thanks!!!

=== Main =============

LibraryLoader loader = new LibraryLoader();
loader.addClassPath(<jar lib root path>);

// below will run biz logic

=== LibraryLoader.java ==========

public class LibraryLoader {

    URLClassLoader urlClassLoader;

    public LibraryLoader() {
        urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    }

    public void addClassPath(String jarLibPath) throws Exception {
        Class urlClass = URLClassLoader.class;
        File jarPath = new File(jarLibPath);

        FileFilter jarFilter = new FileFilter() {
            public boolean accept(File f) {
                if (f.isDirectory())
                    return true;
                String name = f.getName().toLowerCase();
                return name.endsWith("jar");
            }
        };

        File[] files = jarPath.listFiles(jarFilter);
        for (File file : files) {
            if (file.isFile()) {
                URI uriPath = file.toURI();
                URL urlPath = uriPath.toURL();
                Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class });
                method.setAccessible(true);
                method.invoke(urlClassLoader, new Object[] { urlPath });
                System.out.println(file.getCanonicalPath());
            } else {
                addClassPath(file.getCanonicalPath());
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:16:22+00:00Added an answer on June 6, 2026 at 9:16 am

    that’s not what you want to do. even in your stand-alone program, this won’t really work (as you could be pushing multiple versions of the same jar into the main classloader). what you want to do is create a new classloader containing the new jars, then invoke some class in the new classloader.

    e.g.:

    // get relevant jar urls
    URL[] urls = ...;
    
    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    try {
      URLClassLoader loader = new URLClassLoader(urls);
      Thread.currentThread().setContextClassLoader(loader);
    
      Class<?> entryClass = loader.loadClass("entry.class.name");
      // do something here w/ entryClass (e.g. instantiate it) ...
    
    } finally {
      Thread.currentThread().setContextClassLoader(oldLoader);
    }
    

    of course, you probably want to cache these classloaders and re-use them on subsequent requests.

    of course, i’m not sure why you don’t just deploy multiple versions of the servlet with the different versioned jars (since you indicate that the version is part of the servlet path).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stand alone Java Application with Spring 3.0. I am calling REST service
I have a stand alone java application that will be used in a macintosh
I have a stand-alone java application that makes some database read/write business on a
I have a stand alone java application that receives SNMP messages via an SNMP
I have a typical Flex-based application that was written to use a Java/Hibernate/MySQL backend.
We have a situation where we use JSR-330 based injections to configure our stand-alone
I use spring in a stand alone web application, which means there's no servlet
I am developing a stand-alone java application which gathers data from around 1000 measuring
I am looking for alternatives to the Eclipse built-in Java code formatter. Not stand-alone,
I'm asking for a suitable architecture for the following Java web application: The goal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.