Results 1 to 4 of 4
Thread: Class reloading
- 06-13-2009, 12:57 PM #1
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Class reloading
Hello, I have this class to reload classes:
the class BotMethods: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; } }
And the main class:Java Code:import java.net.*; import java.io.*; public class BotMethods { // Only methods }
Which throws this error: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(); } }Why does it throw this error and how to fix it?java.lang.ClassCastException: BotMethods cannot be cast to BotMethods
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,
~MattI die a little on the inside...
Every time I get shot.
- 06-13-2009, 03:02 PM #2
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
- 06-14-2009, 09:35 AM #3
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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.
- 06-14-2009, 07:49 PM #4
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
-
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Modifying and reloading JPanel component
By gopher in forum AWT / SwingReplies: 10Last Post: 02-07-2009, 04:15 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks