Results 1 to 4 of 4

Thread: Class reloading

  1. #1
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default Class reloading

    Hello, I have this class to reload classes:
    Java Code:
    // Ripped from http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html
    
    import java.io.*;
    import java.net.*;
    
    public class MyClassLoader
                 extends ClassLoader
    {
    
        public MyClassLoader(final ClassLoader parent) {
            super(parent);
        }
    
        public Class loadClass(final String name) throws ClassNotFoundException {
            if (!name.equals("BotMethods"))
                return super.loadClass(name);
            System.out.println("IN");
            try {
                String url = "file:BotMethods.class";
                URL myUrl = new URL(url);
                URLConnection connection = myUrl.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();
    
                System.out.println("OUT1");
    
                return defineClass("BotMethods",classData,0,classData.length);
    
            }
            catch (MalformedURLException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("OUT2");
            return null;
        }
    
    }
    the class BotMethods:
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class BotMethods
    {
    
        // Only methods
        
    }
    And the main class:
    Java Code:
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.lang.reflect.*;
    
    
    public class JavaBot
    {
    
      
        private JavaBot() {
            reloadMethods();
        }
        
        public void reloadMethods() {
            try {
                ClassLoader parentClassLoader = MyClassLoader.class.getClassLoader();
                MyClassLoader classLoader = new MyClassLoader(parentClassLoader);
                Class myClass = classLoader.loadClass("BotMethods");
                
                BotMethods bm1 = (BotMethods)myClass.newInstance(); // Error here
    
                //create new class loader so classes can be reloaded.
                classLoader = new MyClassLoader(parentClassLoader);
                myClass = classLoader.loadClass("BotMethods");
    
                bm1 = (BotMethods)myClass.newInstance();
                bm = bm1;
                //bm = new BotMethods();
            }
            catch (Exception e) {
                System.out.println("Could not reload methods");
                e.printStackTrace();
                //BotMethods.say(bout,"Matt","Could not reload methods: "+e);
            }
        }
    
        public static void main(String[] args) {
            new JavaBot();
        }
    }
    Which throws this error:
    java.lang.ClassCastException: BotMethods cannot be cast to BotMethods
    Why does it throw this error and how to fix it?

    To clarify things: I want the class BotMethods to be reloaded without having to restart the application, so that I can add methods and such while running, which can be executed right after I called the reloadMethods method. I know this is possible, I'm just not sure how.

    Please help. :)

    Thanks,

    ~Matt
    I die a little on the inside...
    Every time I get shot.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If you change the methods of a class, it becomes a different class, and you cannot use the new class as if it were the old one.

    What you are doing is incredibly bad practice, and I doubt it is possible. If it is then it shouldn't be.

    If you need this kind of functionality, then explicitly create new classes that subclass BotMethods. This would prevent some of the more heinous type-safety crimes.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    It would be great if it would work.

    Why would I need to subclass BotMethods?
    I die a little on the inside...
    Every time I get shot.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    To make explicit what you are doing and allow the type system to recognise it.

    When you change BotMethods by adding new methods, you are effectively making a new subclass, but you haven't told the JVM this so it won't allow the casts.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  2. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  3. Modifying and reloading JPanel component
    By gopher in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2009, 04:15 AM
  4. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •