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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:14:16+00:00 2026-06-11T13:14:16+00:00

I am creating a class file at run time. I want to replace the

  • 0

I am creating a class file at run time.

I want to replace the existing class file with the updated one in the class loader.

It is similar to hot swapping (e.g. JRebel) which avoids server restart and redeployment.

I found context.xml approach for tomcat for context reloading.
But it is not very useful in case of production environment.

Can we register class with ClassLoader at runtime?
Please suggest if any alternative is there to reload class at runtime.


I am using following code to retrieve current classLoader.

ClassLoader classLoader = LoggingAspect.class.getClassLoader();

Below is the implementation of load class method.

public class AspectClassLoader extends ClassLoader{

@Override
public synchronized Class<?> loadClass(String name) throws ClassNotFoundException
{
    String customLoadClass = "com.log.LoggingAspect";
    try
    {
        if(!customLoadClass.equals(name))
        {
            return super.loadClass(name);
        }
        else
        {
            URL classLoadUrl = new URL(this.reloadClassUrl);
            URLConnection connection = classLoadUrl.openConnection();
            InputStream input = connection.getInputStream();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int data = input.read();

            while(data != -1){
                buffer.write(data);
                data = input.read();
            }

            input.close();

            byte[] classData = buffer.toByteArray();
            return defineClass(name, classData, 0, classData.length);
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null; 
}

To reload class i am using following code.

AspectClassLoader urlClsLoader = new AspectClassLoader(classLoader);
Class aspect = urlClsLoader.reloadClass("com.log.LoggingAspect", true, new String("file:"+className));

My reload method of the classloader is

public synchronized Class<?> reloadClass(String name, boolean resolve, String reloadClassUrl) throws ClassNotFoundException
{
    this.reloadClassUrl = reloadClassUrl;
    return this.loadClass(name, resolve);
}

After reloading if i create the new instance of LoggingAspect it still gives me the old instance. Please suggest.

Class aspect = urlClsLoader.reloadClass("com.log.LoggingAspect", true, new String("file:"+className));
        com.log.aspect.Aspect aspectObj = (com.log.aspect.Aspect)aspect.newInstance();

Still I am getting the old instance.

Please suggest why classloader does not load the modified class?

  • 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-11T13:14:17+00:00Added an answer on June 11, 2026 at 1:14 pm

    This is possible but complex. What you need to do is create a new URLClassLoader with the current ClassLoader as parent. Add the URL to the folder with your class (without package folders) to the new URLClassLoader

    Next, you will have to patch loadClass() to return your new class before calling parent.loadClass() (otherwise, the existing class will be used and the updated one will be ignored).

    It gets complex when you want to use the new class. For this, you will have to create it using the new classloader and make sure that everyone uses this instance of the type.

    It’s probably more safe (and easier to debug) if the class doesn’t exist in your WAR. Instead of using the type, load the class with the URLClassLoader and create an instance of it. So the code using this class works like this:

    IService service = createService();
    

    where IService is an interface which defines the methods that the generated class supports. That way, you can write code which uses the methods without actually needing an implementation.

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

Sidebar

Related Questions

I started creating file downloader class. Here is what i have: public class Downloader
I am working on creating a Java class to map an XML file that
I am creating excel file using below code public class ResultSetToExcel { private HSSFWorkbook
I am creating a class that is mapped to an existing global so it
I want to run an unknown amount (unknown at compile time) of NSTasks and
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PNLResultDAOImpl' defined in file [C:\EclipseWS1\SpringJDBCExample\target\classes\com\synechron\tom\pnl\dao\impl\PNLResultDAOImpl.class]: Instantiation of bean
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
I m creating a class that extends AlertDialog. and setting layout. But when pop
I'm creating a class that creates arrays. (Yes, I realize that's already available in
I'm creating a class. What's the best practice for naming properties/methods when your preferred

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.