Monday, 15 September 2014

java - Reload class using custom class loader -


I am planning to use hot class of class using custom class loader. For this purpose I have written a custom class loader which looks i

  public class custom class loader classload loader {public custom class loader (class load loader parent) {super (parent); } Public class load class (string name) throws classicsonfoundation (if !! !!) "Classloader. Testcase". Axles (name) return super. Load class (name); Try {string url = null; String clzName = null; Url = "file: /home/naveen/workspace/JavaConcept/bin/classLoader/TestCase.class"; ClzName = "classLoader.TestCase"; } Url myUrl = new URL (url); URL Connection Connection = myUrl.openConnection (); InputStream input = connection.getInputStream (); BytereanOutOperputStream buffer = new byteOnputstream (); Int data = input Reed (); While (data! = -1) {buffer.write (data); Data = input Reed (); } Input.close (); Byte [] ClassData = Buffer. To Batteryre (); Return defineClass (clzName, classData, 0, classData.length); } Catch (malmarmdureleuxation E) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); } Return tap; }}  

Now I have another class called Tests, in which there is a method called print ().

  Public Zero Print () {System.out. Println ("hello"); Println ("bye"); }  

And I'm calling it this way with the main method

  throws public static zero main (string AGR []) InstantiationException, IllegalAccessException {TestCase t = New TestCase (); T.print (); {Class Loader Class Loader = Custom Class Loader. Try Clash Gate Classloader (); Classloader = new custom class loader (classloader); Class Clause = Classloader Load class ("Classloader. Test season"); TestCase T2 = New Testing (); T2.print (); } Grip (Klassnotfound Expression E) {e.printStackTrace (); }}  

Now here I want to do one thing which is said inside a main t.print () method which prints hello and goodbye on the console. Now I have another version of this test case class print method which only prints Hello, so what did I do? I told the program in debug mode and programmatically classloader Load class (not allowed to run till line). So I changed testcase.class to the directory structure with the new version, which only prints Hello. But still this production is Hello and showing goodbye. Is anyone wrong in this program or can help me understand the class loader? Please correct me and help complete your work.

Custom Class Loader Because many rules require you to keep in mind, in your case, I think that The problem is that your class file is part of your program, which means that if you try to load a class, then the default class loader will load before calling your load class () method that will ever call will not be done. Try removing your class files from your classpace so that there is no part of your program, then your load class () should be called.

The other problem is that you are creating new by using your testsecuse object, which will call the default class loader, a better way classloader.loadclass () and then make an example by returning the class , But if you put it on tests, it means that the class is defined in your default classloader which leads to the first problem. Alternatively, you have to create an interface and put that method so that your method can be called.


No comments:

Post a Comment