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 6377429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:52:20+00:00 2026-05-25T01:52:20+00:00

I decided to implement dynamic class loading in my glassfish web application, as a

  • 0

I decided to implement dynamic class loading in my glassfish web application, as a way of trying it out and to support small plugins that could be loaded and executed by the web app at runtime.

I added the following class:

public class PluginManager {

   private static final String dropBoxDir = "file:///path/to/dropbox/";
   private static final URLClassLoader dropBoxClassLoader;
   static {
      try {
         URL dropBoxURL = new URL(dropBoxDir);
         dropBoxClassLoader = URLClassLoader.newInstance(new URL[]{dropBoxURL});
      }
      catch (MalformedURLException mue) {
         throw new RuntimeException("MalformedURLException thrown during PluginManager initialization - the hardcoded URL " + dropBoxDir + " must be invalid.", mue);
      }
   }

   //this method is called by a web service
   public static void runPluginFromDropBox(String fullClassName) {
      try {
         //load the plugin class
         Class<?> pluginClass = dropBoxClassLoader.loadClass(fullClassName);
         //instantiate it
         Runnable plugin = (Runnable)pluginClass.newInstance();
         //call its run() method
         plugin.run();
      }
      catch (ClassNotFoundException cnfe) {
         throw new RuntimeException("The class file for " + fullClassName + " could not be located at the designated directory (" + dropBoxDir + "). Check that the specified class name is correct, and that its file is in the right location.", cnfe);
      }
      catch (InstantiationException ie) {
         throw new RuntimeException("InstantiationException thrown when attempting to instantiate the plugin class " + fullClassName + " - make sure it is an instantiable class with a no-arg constructor.", ie);
      }
      catch (IllegalAccessException iae) {
         throw new RuntimeException("IllegalAccessException thrown when attempting to instantiate the plugin class " + fullClassName + " - make sure the class and its no-arg constructor have public access.", iae);
      }
      catch (ClassCastException cce) {
         throw new RuntimeException("Plugin instance could not be cast to Runnable - plugin classes must implement this interface.", cce);
      }
   }
}

Then in a separate project, I created a test plugin:

public class TestPlugin implements Runnable {
   @Override
   public void run() {
      System.out.println("plugin code executed");
   }
}

I deployed the web application, then compiled TestPlugin into a .class file and dropped it into the designated folder. I called a web service that hits runPluginFromDropBox() with the class name and got the expected output.

This all worked as a proof of concept, but my plugin is effectively useless unless it can be made aware of my web application’s classes. I’ve since read that .war is intended only as a standalone application, and not meant to be on other libraries’ classpaths, which doesn’t bode well for this little side-project.

I had a look at this discussion: Extending Java Web Applications with plugins and get the feeling I’m wading into a swamp of design challenges for no huge reason and should turn around. However that post is kind of old and is Tomcat-specific, so I just thought I’d ask if there’s any straightforward way for me to approach this without some elaborate third party framework.

  • 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-05-25T01:52:20+00:00Added an answer on May 25, 2026 at 1:52 am

    The classes of a war file are loaded by a specific classloader, to isolate the war from other webapps deployed on the same server, and to be able to undeploy the war.

    To be aware of the webapp classes, your plugin classloader should have the webapp classloader as its parent. I’m not aware of all the problems you might have by using an additional classloader, but I suspect you might have memory leaks and other nasty problems (static values kept in memory, etc.) when the container will undeploy and redeploy the webapp. And you might have differences between containers as well.

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

Sidebar

Related Questions

I'm about to implement a logging class in C++ and am trying to decide
Objective-C doesn't support methods overloading. Why? Is it doable but Apple decided not implement
I am trying to speed up my Numpy code and decided that I wanted
Why don't they need them, and if someone decided to implement a VM that
I've been developing a Rails application and I've decided to implement a mixture of
I'm in the midst of a university project and have decided to implement a
I'm a beginner (self-learning) programmer learning C++, and recently I decided to implement a
I'm trying to decide how to implement a very basic licensing solution for some
I've decided to implement my own interface for connecting two devices running my game,
I was going through: C++ FAQs about inheritance and decided to implement it just

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.