How to replace classes in a running application in java ?









up vote
18
down vote

favorite
11












Say I have a class named NameGenerator. I can use this to generate names according to a given logic. Then I write a TestNameGeneration class with a method that asks for a letter from the user and generate a name in accordance. Now I want to change the logic in NameGeneration class and apply that particular change without stopping the application.



I did this to learn more about class loaders and can someone please explain the key concepts that I have to learn to do something like that or site any references ?










share|improve this question



























    up vote
    18
    down vote

    favorite
    11












    Say I have a class named NameGenerator. I can use this to generate names according to a given logic. Then I write a TestNameGeneration class with a method that asks for a letter from the user and generate a name in accordance. Now I want to change the logic in NameGeneration class and apply that particular change without stopping the application.



    I did this to learn more about class loaders and can someone please explain the key concepts that I have to learn to do something like that or site any references ?










    share|improve this question

























      up vote
      18
      down vote

      favorite
      11









      up vote
      18
      down vote

      favorite
      11






      11





      Say I have a class named NameGenerator. I can use this to generate names according to a given logic. Then I write a TestNameGeneration class with a method that asks for a letter from the user and generate a name in accordance. Now I want to change the logic in NameGeneration class and apply that particular change without stopping the application.



      I did this to learn more about class loaders and can someone please explain the key concepts that I have to learn to do something like that or site any references ?










      share|improve this question















      Say I have a class named NameGenerator. I can use this to generate names according to a given logic. Then I write a TestNameGeneration class with a method that asks for a letter from the user and generate a name in accordance. Now I want to change the logic in NameGeneration class and apply that particular change without stopping the application.



      I did this to learn more about class loaders and can someone please explain the key concepts that I have to learn to do something like that or site any references ?







      java classloader






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 14 '13 at 10:53









      Mohammad Faisal

      3,394135398




      3,394135398










      asked Jan 14 '13 at 10:47









      Prasad Weera

      5253721




      5253721






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          27
          down vote



          accepted










          Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the file system and calls Test1.hello()



          package test;

          public class Test1
          public void hello()
          System.out.println("Hello !");



          public class Test

          static class TestClassLoader extends ClassLoader
          @Override
          public Class<?> loadClass(String name) throws ClassNotFoundException
          if (name.equals("test.Test1"))
          try
          InputStream is = Test.class.getClassLoader().getResourceAsStream("test/Test1.class");
          byte buf = new byte[10000];
          int len = is.read(buf);
          return defineClass(name, buf, 0, len);
          catch (IOException e)
          throw new ClassNotFoundException("", e);


          return getParent().loadClass(name);



          public static void main(String args) throws Exception
          for (;;)
          Class cls = new TestClassLoader().loadClass("test.Test1");
          Object obj = cls.newInstance();
          cls.getMethod("hello").invoke(obj);
          Thread.sleep(5000);





          Run it. Then change and recompile Test1



          System.out.println("Hello !!!");


          while Test is running. You will see Test1.hello output changed



          ...
          Hello !
          Hello !
          Hello !!!
          Hello !!!


          This is how eg Tomcat reloads webapps. It has a separate ClassLoader for each webapp and loads a new version in a new ClassLoader. The old one is GCed just like any Java object as well as the old classes.



          Note that we loaded Test1 with TestClassLoader and invoked its first method with reflection. But all Test1 dependencies will be implicitly loaded with Test1 class loader, that is all the Test1 application will be loaded by JVM into TestClassLoader.






          share|improve this answer






















          • Thanks for the help. Having new class loader instance for every new class loading as you've done here solved the problem.
            – Prasad Weera
            Jan 14 '13 at 11:38

















          up vote
          2
          down vote













          There are 2 ways:



          1. To overwrite the class loader you're using by first using the existing classloader to bootstrap your application and specifically for the class that you need to have dynamic update, you have to use the overwritten classloader.

          2. To use OSGi framework. Depending on the scale of your application, OSGi framework may not be a good choice as it requires you to follow it's coding convention.

          Hope it helps






          share|improve this answer




















          • +1 for OSGi. It supports adding/ removing bundles at runtime out-of-the-box.
            – Puce
            Jan 14 '13 at 10:58










          • Thanks for the first suggestion. But I don't want to use OSGI as the only intention of this small application is learning core java class loading concepts.
            – Prasad Weera
            Jan 14 '13 at 11:05

















          up vote
          1
          down vote













          It's really simple, you will use of advantage of a OOP and use interface, don't need to manage classloader
          You can inject the implementation with a setter:



          Create an interface called NameGeneration
          Create n implementation NameGenerationImpl1, NameGenerationimpl2 for instance
          In your client class define a variable:



          NameGeneration nameGeneration ;


          and the setter :



           public void setNameGeneration(NameGeneration nameGeneration) 
          this.nameGeneration = nameGeneration ;



          nameGeneration will generate what you want.



          When you change the algorithm you change the implementation by doing for instance :



          setNameGeneration(new NameGenerationImpl1()) ;


          or



          setNameGeneration(new NameGenerationImpl2()) ;





          share|improve this answer






















          • What is POO? I don't get it.
            – Mohammad Faisal
            Jan 14 '13 at 11:06











          • @Mohammad: I think he means OOP...
            – Uwe Plonus
            Jan 14 '13 at 11:10










          • This is basically the strategy pattern.
            – Uwe Plonus
            Jan 14 '13 at 11:10










          • @Uwe Plonus you are right OOP and my example use strategy pattern coupled with dependancy injection.
            – Jean-Christophe Blanchard
            Jan 14 '13 at 11:15

















          up vote
          0
          down vote













          What about a strategy pattern? It could be a better solution for your problem instead of using classloaders.






          share|improve this answer
















          • 1




            How it could help to him dynamically load changed version of classes?
            – Andremoniy
            Jan 14 '13 at 10:51










          • As I know startegy pattern would help to choose between several implementations as we prefer at run time by coding to an interface. But we cannot introduce a whole new implementation n the fly.
            – Prasad Weera
            Jan 14 '13 at 11:02










          • @Prasad you can inject on the fly : see my example above with strategy pattern and DI: setNameGeneration(Class.forName("com.bean.ClasseA").newInstance() )
            – Jean-Christophe Blanchard
            Jan 14 '13 at 11:17


















          up vote
          0
          down vote













          You can write your own custom classloader. When ever there is change in the class file or resource/jar containing the class file (check the timestamp), destroy the previous classloader instance and create a new classloader instance which in turn will load the new class file.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14316996%2fhow-to-replace-classes-in-a-running-application-in-java%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            27
            down vote



            accepted










            Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the file system and calls Test1.hello()



            package test;

            public class Test1
            public void hello()
            System.out.println("Hello !");



            public class Test

            static class TestClassLoader extends ClassLoader
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException
            if (name.equals("test.Test1"))
            try
            InputStream is = Test.class.getClassLoader().getResourceAsStream("test/Test1.class");
            byte buf = new byte[10000];
            int len = is.read(buf);
            return defineClass(name, buf, 0, len);
            catch (IOException e)
            throw new ClassNotFoundException("", e);


            return getParent().loadClass(name);



            public static void main(String args) throws Exception
            for (;;)
            Class cls = new TestClassLoader().loadClass("test.Test1");
            Object obj = cls.newInstance();
            cls.getMethod("hello").invoke(obj);
            Thread.sleep(5000);





            Run it. Then change and recompile Test1



            System.out.println("Hello !!!");


            while Test is running. You will see Test1.hello output changed



            ...
            Hello !
            Hello !
            Hello !!!
            Hello !!!


            This is how eg Tomcat reloads webapps. It has a separate ClassLoader for each webapp and loads a new version in a new ClassLoader. The old one is GCed just like any Java object as well as the old classes.



            Note that we loaded Test1 with TestClassLoader and invoked its first method with reflection. But all Test1 dependencies will be implicitly loaded with Test1 class loader, that is all the Test1 application will be loaded by JVM into TestClassLoader.






            share|improve this answer






















            • Thanks for the help. Having new class loader instance for every new class loading as you've done here solved the problem.
              – Prasad Weera
              Jan 14 '13 at 11:38














            up vote
            27
            down vote



            accepted










            Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the file system and calls Test1.hello()



            package test;

            public class Test1
            public void hello()
            System.out.println("Hello !");



            public class Test

            static class TestClassLoader extends ClassLoader
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException
            if (name.equals("test.Test1"))
            try
            InputStream is = Test.class.getClassLoader().getResourceAsStream("test/Test1.class");
            byte buf = new byte[10000];
            int len = is.read(buf);
            return defineClass(name, buf, 0, len);
            catch (IOException e)
            throw new ClassNotFoundException("", e);


            return getParent().loadClass(name);



            public static void main(String args) throws Exception
            for (;;)
            Class cls = new TestClassLoader().loadClass("test.Test1");
            Object obj = cls.newInstance();
            cls.getMethod("hello").invoke(obj);
            Thread.sleep(5000);





            Run it. Then change and recompile Test1



            System.out.println("Hello !!!");


            while Test is running. You will see Test1.hello output changed



            ...
            Hello !
            Hello !
            Hello !!!
            Hello !!!


            This is how eg Tomcat reloads webapps. It has a separate ClassLoader for each webapp and loads a new version in a new ClassLoader. The old one is GCed just like any Java object as well as the old classes.



            Note that we loaded Test1 with TestClassLoader and invoked its first method with reflection. But all Test1 dependencies will be implicitly loaded with Test1 class loader, that is all the Test1 application will be loaded by JVM into TestClassLoader.






            share|improve this answer






















            • Thanks for the help. Having new class loader instance for every new class loading as you've done here solved the problem.
              – Prasad Weera
              Jan 14 '13 at 11:38












            up vote
            27
            down vote



            accepted







            up vote
            27
            down vote



            accepted






            Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the file system and calls Test1.hello()



            package test;

            public class Test1
            public void hello()
            System.out.println("Hello !");



            public class Test

            static class TestClassLoader extends ClassLoader
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException
            if (name.equals("test.Test1"))
            try
            InputStream is = Test.class.getClassLoader().getResourceAsStream("test/Test1.class");
            byte buf = new byte[10000];
            int len = is.read(buf);
            return defineClass(name, buf, 0, len);
            catch (IOException e)
            throw new ClassNotFoundException("", e);


            return getParent().loadClass(name);



            public static void main(String args) throws Exception
            for (;;)
            Class cls = new TestClassLoader().loadClass("test.Test1");
            Object obj = cls.newInstance();
            cls.getMethod("hello").invoke(obj);
            Thread.sleep(5000);





            Run it. Then change and recompile Test1



            System.out.println("Hello !!!");


            while Test is running. You will see Test1.hello output changed



            ...
            Hello !
            Hello !
            Hello !!!
            Hello !!!


            This is how eg Tomcat reloads webapps. It has a separate ClassLoader for each webapp and loads a new version in a new ClassLoader. The old one is GCed just like any Java object as well as the old classes.



            Note that we loaded Test1 with TestClassLoader and invoked its first method with reflection. But all Test1 dependencies will be implicitly loaded with Test1 class loader, that is all the Test1 application will be loaded by JVM into TestClassLoader.






            share|improve this answer














            Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the file system and calls Test1.hello()



            package test;

            public class Test1
            public void hello()
            System.out.println("Hello !");



            public class Test

            static class TestClassLoader extends ClassLoader
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException
            if (name.equals("test.Test1"))
            try
            InputStream is = Test.class.getClassLoader().getResourceAsStream("test/Test1.class");
            byte buf = new byte[10000];
            int len = is.read(buf);
            return defineClass(name, buf, 0, len);
            catch (IOException e)
            throw new ClassNotFoundException("", e);


            return getParent().loadClass(name);



            public static void main(String args) throws Exception
            for (;;)
            Class cls = new TestClassLoader().loadClass("test.Test1");
            Object obj = cls.newInstance();
            cls.getMethod("hello").invoke(obj);
            Thread.sleep(5000);





            Run it. Then change and recompile Test1



            System.out.println("Hello !!!");


            while Test is running. You will see Test1.hello output changed



            ...
            Hello !
            Hello !
            Hello !!!
            Hello !!!


            This is how eg Tomcat reloads webapps. It has a separate ClassLoader for each webapp and loads a new version in a new ClassLoader. The old one is GCed just like any Java object as well as the old classes.



            Note that we loaded Test1 with TestClassLoader and invoked its first method with reflection. But all Test1 dependencies will be implicitly loaded with Test1 class loader, that is all the Test1 application will be loaded by JVM into TestClassLoader.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 14 '13 at 15:58

























            answered Jan 14 '13 at 11:21









            Evgeniy Dorofeev

            104k23140218




            104k23140218











            • Thanks for the help. Having new class loader instance for every new class loading as you've done here solved the problem.
              – Prasad Weera
              Jan 14 '13 at 11:38
















            • Thanks for the help. Having new class loader instance for every new class loading as you've done here solved the problem.
              – Prasad Weera
              Jan 14 '13 at 11:38















            Thanks for the help. Having new class loader instance for every new class loading as you've done here solved the problem.
            – Prasad Weera
            Jan 14 '13 at 11:38




            Thanks for the help. Having new class loader instance for every new class loading as you've done here solved the problem.
            – Prasad Weera
            Jan 14 '13 at 11:38












            up vote
            2
            down vote













            There are 2 ways:



            1. To overwrite the class loader you're using by first using the existing classloader to bootstrap your application and specifically for the class that you need to have dynamic update, you have to use the overwritten classloader.

            2. To use OSGi framework. Depending on the scale of your application, OSGi framework may not be a good choice as it requires you to follow it's coding convention.

            Hope it helps






            share|improve this answer




















            • +1 for OSGi. It supports adding/ removing bundles at runtime out-of-the-box.
              – Puce
              Jan 14 '13 at 10:58










            • Thanks for the first suggestion. But I don't want to use OSGI as the only intention of this small application is learning core java class loading concepts.
              – Prasad Weera
              Jan 14 '13 at 11:05














            up vote
            2
            down vote













            There are 2 ways:



            1. To overwrite the class loader you're using by first using the existing classloader to bootstrap your application and specifically for the class that you need to have dynamic update, you have to use the overwritten classloader.

            2. To use OSGi framework. Depending on the scale of your application, OSGi framework may not be a good choice as it requires you to follow it's coding convention.

            Hope it helps






            share|improve this answer




















            • +1 for OSGi. It supports adding/ removing bundles at runtime out-of-the-box.
              – Puce
              Jan 14 '13 at 10:58










            • Thanks for the first suggestion. But I don't want to use OSGI as the only intention of this small application is learning core java class loading concepts.
              – Prasad Weera
              Jan 14 '13 at 11:05












            up vote
            2
            down vote










            up vote
            2
            down vote









            There are 2 ways:



            1. To overwrite the class loader you're using by first using the existing classloader to bootstrap your application and specifically for the class that you need to have dynamic update, you have to use the overwritten classloader.

            2. To use OSGi framework. Depending on the scale of your application, OSGi framework may not be a good choice as it requires you to follow it's coding convention.

            Hope it helps






            share|improve this answer












            There are 2 ways:



            1. To overwrite the class loader you're using by first using the existing classloader to bootstrap your application and specifically for the class that you need to have dynamic update, you have to use the overwritten classloader.

            2. To use OSGi framework. Depending on the scale of your application, OSGi framework may not be a good choice as it requires you to follow it's coding convention.

            Hope it helps







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 14 '13 at 10:55









            Wins

            1,68131744




            1,68131744











            • +1 for OSGi. It supports adding/ removing bundles at runtime out-of-the-box.
              – Puce
              Jan 14 '13 at 10:58










            • Thanks for the first suggestion. But I don't want to use OSGI as the only intention of this small application is learning core java class loading concepts.
              – Prasad Weera
              Jan 14 '13 at 11:05
















            • +1 for OSGi. It supports adding/ removing bundles at runtime out-of-the-box.
              – Puce
              Jan 14 '13 at 10:58










            • Thanks for the first suggestion. But I don't want to use OSGI as the only intention of this small application is learning core java class loading concepts.
              – Prasad Weera
              Jan 14 '13 at 11:05















            +1 for OSGi. It supports adding/ removing bundles at runtime out-of-the-box.
            – Puce
            Jan 14 '13 at 10:58




            +1 for OSGi. It supports adding/ removing bundles at runtime out-of-the-box.
            – Puce
            Jan 14 '13 at 10:58












            Thanks for the first suggestion. But I don't want to use OSGI as the only intention of this small application is learning core java class loading concepts.
            – Prasad Weera
            Jan 14 '13 at 11:05




            Thanks for the first suggestion. But I don't want to use OSGI as the only intention of this small application is learning core java class loading concepts.
            – Prasad Weera
            Jan 14 '13 at 11:05










            up vote
            1
            down vote













            It's really simple, you will use of advantage of a OOP and use interface, don't need to manage classloader
            You can inject the implementation with a setter:



            Create an interface called NameGeneration
            Create n implementation NameGenerationImpl1, NameGenerationimpl2 for instance
            In your client class define a variable:



            NameGeneration nameGeneration ;


            and the setter :



             public void setNameGeneration(NameGeneration nameGeneration) 
            this.nameGeneration = nameGeneration ;



            nameGeneration will generate what you want.



            When you change the algorithm you change the implementation by doing for instance :



            setNameGeneration(new NameGenerationImpl1()) ;


            or



            setNameGeneration(new NameGenerationImpl2()) ;





            share|improve this answer






















            • What is POO? I don't get it.
              – Mohammad Faisal
              Jan 14 '13 at 11:06











            • @Mohammad: I think he means OOP...
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • This is basically the strategy pattern.
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • @Uwe Plonus you are right OOP and my example use strategy pattern coupled with dependancy injection.
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:15














            up vote
            1
            down vote













            It's really simple, you will use of advantage of a OOP and use interface, don't need to manage classloader
            You can inject the implementation with a setter:



            Create an interface called NameGeneration
            Create n implementation NameGenerationImpl1, NameGenerationimpl2 for instance
            In your client class define a variable:



            NameGeneration nameGeneration ;


            and the setter :



             public void setNameGeneration(NameGeneration nameGeneration) 
            this.nameGeneration = nameGeneration ;



            nameGeneration will generate what you want.



            When you change the algorithm you change the implementation by doing for instance :



            setNameGeneration(new NameGenerationImpl1()) ;


            or



            setNameGeneration(new NameGenerationImpl2()) ;





            share|improve this answer






















            • What is POO? I don't get it.
              – Mohammad Faisal
              Jan 14 '13 at 11:06











            • @Mohammad: I think he means OOP...
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • This is basically the strategy pattern.
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • @Uwe Plonus you are right OOP and my example use strategy pattern coupled with dependancy injection.
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:15












            up vote
            1
            down vote










            up vote
            1
            down vote









            It's really simple, you will use of advantage of a OOP and use interface, don't need to manage classloader
            You can inject the implementation with a setter:



            Create an interface called NameGeneration
            Create n implementation NameGenerationImpl1, NameGenerationimpl2 for instance
            In your client class define a variable:



            NameGeneration nameGeneration ;


            and the setter :



             public void setNameGeneration(NameGeneration nameGeneration) 
            this.nameGeneration = nameGeneration ;



            nameGeneration will generate what you want.



            When you change the algorithm you change the implementation by doing for instance :



            setNameGeneration(new NameGenerationImpl1()) ;


            or



            setNameGeneration(new NameGenerationImpl2()) ;





            share|improve this answer














            It's really simple, you will use of advantage of a OOP and use interface, don't need to manage classloader
            You can inject the implementation with a setter:



            Create an interface called NameGeneration
            Create n implementation NameGenerationImpl1, NameGenerationimpl2 for instance
            In your client class define a variable:



            NameGeneration nameGeneration ;


            and the setter :



             public void setNameGeneration(NameGeneration nameGeneration) 
            this.nameGeneration = nameGeneration ;



            nameGeneration will generate what you want.



            When you change the algorithm you change the implementation by doing for instance :



            setNameGeneration(new NameGenerationImpl1()) ;


            or



            setNameGeneration(new NameGenerationImpl2()) ;






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 14 '13 at 11:12

























            answered Jan 14 '13 at 10:56









            Jean-Christophe Blanchard

            39929




            39929











            • What is POO? I don't get it.
              – Mohammad Faisal
              Jan 14 '13 at 11:06











            • @Mohammad: I think he means OOP...
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • This is basically the strategy pattern.
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • @Uwe Plonus you are right OOP and my example use strategy pattern coupled with dependancy injection.
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:15
















            • What is POO? I don't get it.
              – Mohammad Faisal
              Jan 14 '13 at 11:06











            • @Mohammad: I think he means OOP...
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • This is basically the strategy pattern.
              – Uwe Plonus
              Jan 14 '13 at 11:10










            • @Uwe Plonus you are right OOP and my example use strategy pattern coupled with dependancy injection.
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:15















            What is POO? I don't get it.
            – Mohammad Faisal
            Jan 14 '13 at 11:06





            What is POO? I don't get it.
            – Mohammad Faisal
            Jan 14 '13 at 11:06













            @Mohammad: I think he means OOP...
            – Uwe Plonus
            Jan 14 '13 at 11:10




            @Mohammad: I think he means OOP...
            – Uwe Plonus
            Jan 14 '13 at 11:10












            This is basically the strategy pattern.
            – Uwe Plonus
            Jan 14 '13 at 11:10




            This is basically the strategy pattern.
            – Uwe Plonus
            Jan 14 '13 at 11:10












            @Uwe Plonus you are right OOP and my example use strategy pattern coupled with dependancy injection.
            – Jean-Christophe Blanchard
            Jan 14 '13 at 11:15




            @Uwe Plonus you are right OOP and my example use strategy pattern coupled with dependancy injection.
            – Jean-Christophe Blanchard
            Jan 14 '13 at 11:15










            up vote
            0
            down vote













            What about a strategy pattern? It could be a better solution for your problem instead of using classloaders.






            share|improve this answer
















            • 1




              How it could help to him dynamically load changed version of classes?
              – Andremoniy
              Jan 14 '13 at 10:51










            • As I know startegy pattern would help to choose between several implementations as we prefer at run time by coding to an interface. But we cannot introduce a whole new implementation n the fly.
              – Prasad Weera
              Jan 14 '13 at 11:02










            • @Prasad you can inject on the fly : see my example above with strategy pattern and DI: setNameGeneration(Class.forName("com.bean.ClasseA").newInstance() )
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:17















            up vote
            0
            down vote













            What about a strategy pattern? It could be a better solution for your problem instead of using classloaders.






            share|improve this answer
















            • 1




              How it could help to him dynamically load changed version of classes?
              – Andremoniy
              Jan 14 '13 at 10:51










            • As I know startegy pattern would help to choose between several implementations as we prefer at run time by coding to an interface. But we cannot introduce a whole new implementation n the fly.
              – Prasad Weera
              Jan 14 '13 at 11:02










            • @Prasad you can inject on the fly : see my example above with strategy pattern and DI: setNameGeneration(Class.forName("com.bean.ClasseA").newInstance() )
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:17













            up vote
            0
            down vote










            up vote
            0
            down vote









            What about a strategy pattern? It could be a better solution for your problem instead of using classloaders.






            share|improve this answer












            What about a strategy pattern? It could be a better solution for your problem instead of using classloaders.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 14 '13 at 10:49









            Uwe Plonus

            8,01222640




            8,01222640







            • 1




              How it could help to him dynamically load changed version of classes?
              – Andremoniy
              Jan 14 '13 at 10:51










            • As I know startegy pattern would help to choose between several implementations as we prefer at run time by coding to an interface. But we cannot introduce a whole new implementation n the fly.
              – Prasad Weera
              Jan 14 '13 at 11:02










            • @Prasad you can inject on the fly : see my example above with strategy pattern and DI: setNameGeneration(Class.forName("com.bean.ClasseA").newInstance() )
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:17













            • 1




              How it could help to him dynamically load changed version of classes?
              – Andremoniy
              Jan 14 '13 at 10:51










            • As I know startegy pattern would help to choose between several implementations as we prefer at run time by coding to an interface. But we cannot introduce a whole new implementation n the fly.
              – Prasad Weera
              Jan 14 '13 at 11:02










            • @Prasad you can inject on the fly : see my example above with strategy pattern and DI: setNameGeneration(Class.forName("com.bean.ClasseA").newInstance() )
              – Jean-Christophe Blanchard
              Jan 14 '13 at 11:17








            1




            1




            How it could help to him dynamically load changed version of classes?
            – Andremoniy
            Jan 14 '13 at 10:51




            How it could help to him dynamically load changed version of classes?
            – Andremoniy
            Jan 14 '13 at 10:51












            As I know startegy pattern would help to choose between several implementations as we prefer at run time by coding to an interface. But we cannot introduce a whole new implementation n the fly.
            – Prasad Weera
            Jan 14 '13 at 11:02




            As I know startegy pattern would help to choose between several implementations as we prefer at run time by coding to an interface. But we cannot introduce a whole new implementation n the fly.
            – Prasad Weera
            Jan 14 '13 at 11:02












            @Prasad you can inject on the fly : see my example above with strategy pattern and DI: setNameGeneration(Class.forName("com.bean.ClasseA").newInstance() )
            – Jean-Christophe Blanchard
            Jan 14 '13 at 11:17





            @Prasad you can inject on the fly : see my example above with strategy pattern and DI: setNameGeneration(Class.forName("com.bean.ClasseA").newInstance() )
            – Jean-Christophe Blanchard
            Jan 14 '13 at 11:17











            up vote
            0
            down vote













            You can write your own custom classloader. When ever there is change in the class file or resource/jar containing the class file (check the timestamp), destroy the previous classloader instance and create a new classloader instance which in turn will load the new class file.






            share|improve this answer
























              up vote
              0
              down vote













              You can write your own custom classloader. When ever there is change in the class file or resource/jar containing the class file (check the timestamp), destroy the previous classloader instance and create a new classloader instance which in turn will load the new class file.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                You can write your own custom classloader. When ever there is change in the class file or resource/jar containing the class file (check the timestamp), destroy the previous classloader instance and create a new classloader instance which in turn will load the new class file.






                share|improve this answer












                You can write your own custom classloader. When ever there is change in the class file or resource/jar containing the class file (check the timestamp), destroy the previous classloader instance and create a new classloader instance which in turn will load the new class file.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 14 '13 at 14:39









                Abhay Yadav

                635




                635



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14316996%2fhow-to-replace-classes-in-a-running-application-in-java%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric