Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ClassLoader not loading recompiled classes

Reply
Thread Tools

ClassLoader not loading recompiled classes

 
 
Aryeh M. Friedman
Guest
Posts: n/a
 
      10-02-2007
ClassLoader does not update class on recompile:

Script started on Tue Oct 2 01:45:20 200
> cat Main.java

public class Main
{
public static void main(String[] args)
throws Throwable
{
while(true) {
ClassLoader loader=ClassLoader.getSystemClassLoader();
Class klass=loader.loadClass("MyClass");
MyClass mc=(MyClass) klass.newInstance();

System.out.println("hit any key to reload/rerun MyClass");
System.in.read();
}
}
}
> cat MyClass.java

public class MyClass
{
public MyClass()
{
System.out.println("not hi there");
}
}
> javac *.java
> java Main

not hi there
hit any key to reload/rerun MyClass

not hi there
hit any key to reload/rerun MyClass
^Z
Suspended
> cat foo

public class MyClass
{
public MyClass()
{
System.out.println("foo on you");
}
}
> mv foo MyClass.java
> javac MyClass.java
> fg

java Main

not hi there
hit any key to reload/rerun MyClass
^C
> exit

Script done on Tue Oct 2 01:47:02 200

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      10-02-2007
On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
wrote:
> ClassLoader does not update class on recompile:
>
> Script started on Tue Oct 2 01:45:20 200> cat Main.java
>
> public class Main
> {
> public static void main(String[] args)
> throws Throwable
> {
> while(true) {
> ClassLoader loader=ClassLoader.getSystemClassLoader();
> Class klass=loader.loadClass("MyClass");
> MyClass mc=(MyClass) klass.newInstance();
>
> System.out.println("hit any key to reload/rerun MyClass");
> System.in.read();
> }
> }}
> > cat MyClass.java

>
> public class MyClass
> {
> public MyClass()
> {
> System.out.println("not hi there");
> }}
> > javac *.java
> > java Main

>
> not hi there
> hit any key to reload/rerun MyClass
>
> not hi there
> hit any key to reload/rerun MyClass
> ^Z
> Suspended> cat foo
>
> public class MyClass
> {
> public MyClass()
> {
> System.out.println("foo on you");
> }}
> > mv foo MyClass.java
> > javac MyClass.java
> > fg

>
> java Main
>
> not hi there
> hit any key to reload/rerun MyClass
> ^C> exit
>
> Script done on Tue Oct 2 01:47:02 200


Right, a ClassLoader will not re-load a class. You will have to
instantiate a new class loader to do so.
ClassLoader.loadClass will first look for already loaded classes. It
will not re-load the class into the JVM.

Its generally difficult to get dynamic class behavior from Java. You
aren't able to unload a class, and load a different version of it.
Also, and already loaded classes that refer to that other class will
only be able to refer to one instance of it, not one from one class
loader, and then another from another class loader.


 
Reply With Quote
 
 
 
 
Aryeh M. Friedman
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
> On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
> wrote:
>
>
>
> > ClassLoader does not update class on recompile:

>
> > Script started on Tue Oct 2 01:45:20 200> cat Main.java

>
> > public class Main
> > {
> > public static void main(String[] args)
> > throws Throwable
> > {
> > while(true) {
> > ClassLoader loader=ClassLoader.getSystemClassLoader();
> > Class klass=loader.loadClass("MyClass");
> > MyClass mc=(MyClass) klass.newInstance();

>
> > System.out.println("hit any key to reload/rerun MyClass");
> > System.in.read();
> > }
> > }}
> > > cat MyClass.java

>
> > public class MyClass
> > {
> > public MyClass()
> > {
> > System.out.println("not hi there");
> > }}
> > > javac *.java
> > > java Main

>
> > not hi there
> > hit any key to reload/rerun MyClass

>
> > not hi there
> > hit any key to reload/rerun MyClass
> > ^Z
> > Suspended> cat foo

>
> > public class MyClass
> > {
> > public MyClass()
> > {
> > System.out.println("foo on you");
> > }}
> > > mv foo MyClass.java
> > > javac MyClass.java
> > > fg

>
> > java Main

>
> > not hi there
> > hit any key to reload/rerun MyClass
> > ^C> exit

>
> > Script done on Tue Oct 2 01:47:02 200

>
> Right, a ClassLoader will not re-load a class. You will have to
> instantiate a new class loader to do so.
> ClassLoader.loadClass will first look for already loaded classes. It
> will not re-load the class into the JVM.
>
> Its generally difficult to get dynamic class behavior from Java. You
> aren't able to unload a class, and load a different version of it.
> Also, and already loaded classes that refer to that other class will
> only be able to refer to one instance of it, not one from one class
> loader, and then another from another class loader.


Since I am relativally naive with class loaders how do I create a new
instance of the system class loader?

 
Reply With Quote
 
Silvio Bierman
Guest
Posts: n/a
 
      10-02-2007
Aryeh M. Friedman wrote:
> On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
>> On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
>> wrote:
>>
>>
>>
>>> ClassLoader does not update class on recompile:
>>> Script started on Tue Oct 2 01:45:20 200> cat Main.java
>>> public class Main
>>> {
>>> public static void main(String[] args)
>>> throws Throwable
>>> {
>>> while(true) {
>>> ClassLoader loader=ClassLoader.getSystemClassLoader();
>>> Class klass=loader.loadClass("MyClass");
>>> MyClass mc=(MyClass) klass.newInstance();
>>> System.out.println("hit any key to reload/rerun MyClass");
>>> System.in.read();
>>> }
>>> }}
>>>> cat MyClass.java
>>> public class MyClass
>>> {
>>> public MyClass()
>>> {
>>> System.out.println("not hi there");
>>> }}
>>>> javac *.java
>>>> java Main
>>> not hi there
>>> hit any key to reload/rerun MyClass
>>> not hi there
>>> hit any key to reload/rerun MyClass
>>> ^Z
>>> Suspended> cat foo
>>> public class MyClass
>>> {
>>> public MyClass()
>>> {
>>> System.out.println("foo on you");
>>> }}
>>>> mv foo MyClass.java
>>>> javac MyClass.java
>>>> fg
>>> java Main
>>> not hi there
>>> hit any key to reload/rerun MyClass
>>> ^C> exit
>>> Script done on Tue Oct 2 01:47:02 200

>> Right, a ClassLoader will not re-load a class. You will have to
>> instantiate a new class loader to do so.
>> ClassLoader.loadClass will first look for already loaded classes. It
>> will not re-load the class into the JVM.
>>
>> Its generally difficult to get dynamic class behavior from Java. You
>> aren't able to unload a class, and load a different version of it.
>> Also, and already loaded classes that refer to that other class will
>> only be able to refer to one instance of it, not one from one class
>> loader, and then another from another class loader.

>
> Since I am relativally naive with class loaders how do I create a new
> instance of the system class loader?
>


You don't. You can write your own class loaders if you want and can
implement any loading behavior you think is suitable.

Beware that class-reloading introduces all kinds of behavior that is
counter-intuitive. Multiple instances of what appears to be the same
static variable thereby also breaking singleton patterns in your code
(which are broken already, but that is a completely different matter) is
only one example.

Please start by telling us what the real problem is that you need to
solve. Going the class-loader way is probably among the worst solutions
for your problem.

Regards,

Silvio Bierman
 
Reply With Quote
 
Aryeh M. Friedman
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 7:56 am, Silvio Bierman <(E-Mail Removed)> wrote:
> Aryeh M. Friedman wrote:
> > On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
> >> On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
> >> wrote:

>
> >>> ClassLoader does not update class on recompile:
> >>> Script started on Tue Oct 2 01:45:20 200> cat Main.java
> >>> public class Main
> >>> {
> >>> public static void main(String[] args)
> >>> throws Throwable
> >>> {
> >>> while(true) {
> >>> ClassLoader loader=ClassLoader.getSystemClassLoader();
> >>> Class klass=loader.loadClass("MyClass");
> >>> MyClass mc=(MyClass) klass.newInstance();
> >>> System.out.println("hit any key to reload/rerun MyClass");
> >>> System.in.read();
> >>> }
> >>> }}
> >>>> cat MyClass.java
> >>> public class MyClass
> >>> {
> >>> public MyClass()
> >>> {
> >>> System.out.println("not hi there");
> >>> }}
> >>>> javac *.java
> >>>> java Main
> >>> not hi there
> >>> hit any key to reload/rerun MyClass
> >>> not hi there
> >>> hit any key to reload/rerun MyClass
> >>> ^Z
> >>> Suspended> cat foo
> >>> public class MyClass
> >>> {
> >>> public MyClass()
> >>> {
> >>> System.out.println("foo on you");
> >>> }}
> >>>> mv foo MyClass.java
> >>>> javac MyClass.java
> >>>> fg
> >>> java Main
> >>> not hi there
> >>> hit any key to reload/rerun MyClass
> >>> ^C> exit
> >>> Script done on Tue Oct 2 01:47:02 200
> >> Right, a ClassLoader will not re-load a class. You will have to
> >> instantiate a new class loader to do so.
> >> ClassLoader.loadClass will first look for already loaded classes. It
> >> will not re-load the class into the JVM.

>
> >> Its generally difficult to get dynamic class behavior from Java. You
> >> aren't able to unload a class, and load a different version of it.
> >> Also, and already loaded classes that refer to that other class will
> >> only be able to refer to one instance of it, not one from one class
> >> loader, and then another from another class loader.

>
> > Since I am relativally naive with class loaders how do I create a new
> > instance of the system class loader?

>
> You don't. You can write your own class loaders if you want and can
> implement any loading behavior you think is suitable.
>
> Beware that class-reloading introduces all kinds of behavior that is
> counter-intuitive. Multiple instances of what appears to be the same
> static variable thereby also breaking singleton patterns in your code
> (which are broken already, but that is a completely different matter) is
> only one example.
>
> Please start by telling us what the real problem is that you need to
> solve. Going the class-loader way is probably among the worst solutions
> for your problem.


I wrote a gui based unit testing framework and it loads the top level
test suites from a text box with there names in it (initially
populated from command line)... I do not want to have to close/reopen
the app when I rewrite/recompile some code under test.

--Aryeh

 
Reply With Quote
 
Aryeh M. Friedman
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 9:04 am, "Aryeh M. Friedman" <(E-Mail Removed)>
wrote:
> On Oct 2, 7:56 am, Silvio Bierman <(E-Mail Removed)> wrote:
>
>
>
> > Aryeh M. Friedman wrote:
> > > On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
> > >> On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
> > >> wrote:

>
> > >>> ClassLoader does not update class on recompile:
> > >>> Script started on Tue Oct 2 01:45:20 200> cat Main.java
> > >>> public class Main
> > >>> {
> > >>> public static void main(String[] args)
> > >>> throws Throwable
> > >>> {
> > >>> while(true) {
> > >>> ClassLoader loader=ClassLoader.getSystemClassLoader();
> > >>> Class klass=loader.loadClass("MyClass");
> > >>> MyClass mc=(MyClass) klass.newInstance();
> > >>> System.out.println("hit any key to reload/rerun MyClass");
> > >>> System.in.read();
> > >>> }
> > >>> }}
> > >>>> cat MyClass.java
> > >>> public class MyClass
> > >>> {
> > >>> public MyClass()
> > >>> {
> > >>> System.out.println("not hi there");
> > >>> }}
> > >>>> javac *.java
> > >>>> java Main
> > >>> not hi there
> > >>> hit any key to reload/rerun MyClass
> > >>> not hi there
> > >>> hit any key to reload/rerun MyClass
> > >>> ^Z
> > >>> Suspended> cat foo
> > >>> public class MyClass
> > >>> {
> > >>> public MyClass()
> > >>> {
> > >>> System.out.println("foo on you");
> > >>> }}
> > >>>> mv foo MyClass.java
> > >>>> javac MyClass.java
> > >>>> fg
> > >>> java Main
> > >>> not hi there
> > >>> hit any key to reload/rerun MyClass
> > >>> ^C> exit
> > >>> Script done on Tue Oct 2 01:47:02 200
> > >> Right, a ClassLoader will not re-load a class. You will have to
> > >> instantiate a new class loader to do so.
> > >> ClassLoader.loadClass will first look for already loaded classes. It
> > >> will not re-load the class into the JVM.

>
> > >> Its generally difficult to get dynamic class behavior from Java. You
> > >> aren't able to unload a class, and load a different version of it.
> > >> Also, and already loaded classes that refer to that other class will
> > >> only be able to refer to one instance of it, not one from one class
> > >> loader, and then another from another class loader.

>
> > > Since I am relativally naive with class loaders how do I create a new
> > > instance of the system class loader?

>
> > You don't. You can write your own class loaders if you want and can
> > implement any loading behavior you think is suitable.

>
> > Beware that class-reloading introduces all kinds of behavior that is
> > counter-intuitive. Multiple instances of what appears to be the same
> > static variable thereby also breaking singleton patterns in your code
> > (which are broken already, but that is a completely different matter) is
> > only one example.

>
> > Please start by telling us what the real problem is that you need to
> > solve. Going the class-loader way is probably among the worst solutions
> > for your problem.

>
> I wrote a gui based unit testing framework and it loads the top level
> test suites from a text box with there names in it (initially
> populated from command line)... I do not want to have to close/reopen
> the app when I rewrite/recompile some code under test.


If you want to see the actual thing in action you can download it from
http://www.flosoft-systems.com/order.php (sorry but due to legal
restrictions I can not give out the direct download link... for
"purpose" just put in "classloader issue from c.l.j.p" and the rest of
the data I can careless about [unless your of course going to buy it
after the trial peroid is over ;=)]

 
Reply With Quote
 
Silvio Bierman
Guest
Posts: n/a
 
      10-02-2007
Aryeh M. Friedman wrote:
> On Oct 2, 7:56 am, Silvio Bierman <(E-Mail Removed)> wrote:
>> Aryeh M. Friedman wrote:
>>> On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
>>>> On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
>>>> wrote:
>>>>> ClassLoader does not update class on recompile:
>>>>> Script started on Tue Oct 2 01:45:20 200> cat Main.java
>>>>> public class Main
>>>>> {
>>>>> public static void main(String[] args)
>>>>> throws Throwable
>>>>> {
>>>>> while(true) {
>>>>> ClassLoader loader=ClassLoader.getSystemClassLoader();
>>>>> Class klass=loader.loadClass("MyClass");
>>>>> MyClass mc=(MyClass) klass.newInstance();
>>>>> System.out.println("hit any key to reload/rerun MyClass");
>>>>> System.in.read();
>>>>> }
>>>>> }}
>>>>>> cat MyClass.java
>>>>> public class MyClass
>>>>> {
>>>>> public MyClass()
>>>>> {
>>>>> System.out.println("not hi there");
>>>>> }}
>>>>>> javac *.java
>>>>>> java Main
>>>>> not hi there
>>>>> hit any key to reload/rerun MyClass
>>>>> not hi there
>>>>> hit any key to reload/rerun MyClass
>>>>> ^Z
>>>>> Suspended> cat foo
>>>>> public class MyClass
>>>>> {
>>>>> public MyClass()
>>>>> {
>>>>> System.out.println("foo on you");
>>>>> }}
>>>>>> mv foo MyClass.java
>>>>>> javac MyClass.java
>>>>>> fg
>>>>> java Main
>>>>> not hi there
>>>>> hit any key to reload/rerun MyClass
>>>>> ^C> exit
>>>>> Script done on Tue Oct 2 01:47:02 200
>>>> Right, a ClassLoader will not re-load a class. You will have to
>>>> instantiate a new class loader to do so.
>>>> ClassLoader.loadClass will first look for already loaded classes. It
>>>> will not re-load the class into the JVM.
>>>> Its generally difficult to get dynamic class behavior from Java. You
>>>> aren't able to unload a class, and load a different version of it.
>>>> Also, and already loaded classes that refer to that other class will
>>>> only be able to refer to one instance of it, not one from one class
>>>> loader, and then another from another class loader.
>>> Since I am relativally naive with class loaders how do I create a new
>>> instance of the system class loader?

>> You don't. You can write your own class loaders if you want and can
>> implement any loading behavior you think is suitable.
>>
>> Beware that class-reloading introduces all kinds of behavior that is
>> counter-intuitive. Multiple instances of what appears to be the same
>> static variable thereby also breaking singleton patterns in your code
>> (which are broken already, but that is a completely different matter) is
>> only one example.
>>
>> Please start by telling us what the real problem is that you need to
>> solve. Going the class-loader way is probably among the worst solutions
>> for your problem.

>
> I wrote a gui based unit testing framework and it loads the top level
> test suites from a text box with there names in it (initially
> populated from command line)... I do not want to have to close/reopen
> the app when I rewrite/recompile some code under test.
>
> --Aryeh
>


Is there any need to run the tests in-process? You could spawn an
external java process to run the tests and have it store the test
results in a place where the GUI can find it.

Gr.

Silvio

 
Reply With Quote
 
Aryeh M. Friedman
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 9:28 am, Silvio Bierman <(E-Mail Removed)> wrote:
> Aryeh M. Friedman wrote:
> > On Oct 2, 7:56 am, Silvio Bierman <(E-Mail Removed)> wrote:
> >> Aryeh M. Friedman wrote:
> >>> On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
> >>>> On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
> >>>> wrote:
> >>>>> ClassLoader does not update class on recompile:
> >>>>> Script started on Tue Oct 2 01:45:20 200> cat Main.java
> >>>>> public class Main
> >>>>> {
> >>>>> public static void main(String[] args)
> >>>>> throws Throwable
> >>>>> {
> >>>>> while(true) {
> >>>>> ClassLoader loader=ClassLoader.getSystemClassLoader();
> >>>>> Class klass=loader.loadClass("MyClass");
> >>>>> MyClass mc=(MyClass) klass.newInstance();
> >>>>> System.out.println("hit any key to reload/rerun MyClass");
> >>>>> System.in.read();
> >>>>> }
> >>>>> }}
> >>>>>> cat MyClass.java
> >>>>> public class MyClass
> >>>>> {
> >>>>> public MyClass()
> >>>>> {
> >>>>> System.out.println("not hi there");
> >>>>> }}
> >>>>>> javac *.java
> >>>>>> java Main
> >>>>> not hi there
> >>>>> hit any key to reload/rerun MyClass
> >>>>> not hi there
> >>>>> hit any key to reload/rerun MyClass
> >>>>> ^Z
> >>>>> Suspended> cat foo
> >>>>> public class MyClass
> >>>>> {
> >>>>> public MyClass()
> >>>>> {
> >>>>> System.out.println("foo on you");
> >>>>> }}
> >>>>>> mv foo MyClass.java
> >>>>>> javac MyClass.java
> >>>>>> fg
> >>>>> java Main
> >>>>> not hi there
> >>>>> hit any key to reload/rerun MyClass
> >>>>> ^C> exit
> >>>>> Script done on Tue Oct 2 01:47:02 200
> >>>> Right, a ClassLoader will not re-load a class. You will have to
> >>>> instantiate a new class loader to do so.
> >>>> ClassLoader.loadClass will first look for already loaded classes. It
> >>>> will not re-load the class into the JVM.
> >>>> Its generally difficult to get dynamic class behavior from Java. You
> >>>> aren't able to unload a class, and load a different version of it.
> >>>> Also, and already loaded classes that refer to that other class will
> >>>> only be able to refer to one instance of it, not one from one class
> >>>> loader, and then another from another class loader.
> >>> Since I am relativally naive with class loaders how do I create a new
> >>> instance of the system class loader?
> >> You don't. You can write your own class loaders if you want and can
> >> implement any loading behavior you think is suitable.

>
> >> Beware that class-reloading introduces all kinds of behavior that is
> >> counter-intuitive. Multiple instances of what appears to be the same
> >> static variable thereby also breaking singleton patterns in your code
> >> (which are broken already, but that is a completely different matter) is
> >> only one example.

>
> >> Please start by telling us what the real problem is that you need to
> >> solve. Going the class-loader way is probably among the worst solutions
> >> for your problem.

>
> > I wrote a gui based unit testing framework and it loads the top level
> > test suites from a text box with there names in it (initially
> > populated from command line)... I do not want to have to close/reopen
> > the app when I rewrite/recompile some code under test.

>
> > --Aryeh

>
> Is there any need to run the tests in-process? You could spawn an
> external java process to run the tests and have it store the test
> results in a place where the GUI can find it.
>
> Gr.
>
> Silvio


In theory no but it sounds a little kludgist and perhaps not portable
(I don't see how it wouldn't be but once you ask the OS for anything I
get nervious)

 
Reply With Quote
 
Silvio Bierman
Guest
Posts: n/a
 
      10-02-2007
Aryeh M. Friedman wrote:
> On Oct 2, 9:28 am, Silvio Bierman <(E-Mail Removed)> wrote:
>> Aryeh M. Friedman wrote:
>>> On Oct 2, 7:56 am, Silvio Bierman <(E-Mail Removed)> wrote:
>>>> Aryeh M. Friedman wrote:
>>>>> On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
>>>>>> On Oct 1, 10:53 pm, "Aryeh M. Friedman" <(E-Mail Removed)>
>>>>>> wrote:
>>>>>>> ClassLoader does not update class on recompile:
>>>>>>> Script started on Tue Oct 2 01:45:20 200> cat Main.java
>>>>>>> public class Main
>>>>>>> {
>>>>>>> public static void main(String[] args)
>>>>>>> throws Throwable
>>>>>>> {
>>>>>>> while(true) {
>>>>>>> ClassLoader loader=ClassLoader.getSystemClassLoader();
>>>>>>> Class klass=loader.loadClass("MyClass");
>>>>>>> MyClass mc=(MyClass) klass.newInstance();
>>>>>>> System.out.println("hit any key to reload/rerun MyClass");
>>>>>>> System.in.read();
>>>>>>> }
>>>>>>> }}
>>>>>>>> cat MyClass.java
>>>>>>> public class MyClass
>>>>>>> {
>>>>>>> public MyClass()
>>>>>>> {
>>>>>>> System.out.println("not hi there");
>>>>>>> }}
>>>>>>>> javac *.java
>>>>>>>> java Main
>>>>>>> not hi there
>>>>>>> hit any key to reload/rerun MyClass
>>>>>>> not hi there
>>>>>>> hit any key to reload/rerun MyClass
>>>>>>> ^Z
>>>>>>> Suspended> cat foo
>>>>>>> public class MyClass
>>>>>>> {
>>>>>>> public MyClass()
>>>>>>> {
>>>>>>> System.out.println("foo on you");
>>>>>>> }}
>>>>>>>> mv foo MyClass.java
>>>>>>>> javac MyClass.java
>>>>>>>> fg
>>>>>>> java Main
>>>>>>> not hi there
>>>>>>> hit any key to reload/rerun MyClass
>>>>>>> ^C> exit
>>>>>>> Script done on Tue Oct 2 01:47:02 200
>>>>>> Right, a ClassLoader will not re-load a class. You will have to
>>>>>> instantiate a new class loader to do so.
>>>>>> ClassLoader.loadClass will first look for already loaded classes. It
>>>>>> will not re-load the class into the JVM.
>>>>>> Its generally difficult to get dynamic class behavior from Java. You
>>>>>> aren't able to unload a class, and load a different version of it.
>>>>>> Also, and already loaded classes that refer to that other class will
>>>>>> only be able to refer to one instance of it, not one from one class
>>>>>> loader, and then another from another class loader.
>>>>> Since I am relativally naive with class loaders how do I create a new
>>>>> instance of the system class loader?
>>>> You don't. You can write your own class loaders if you want and can
>>>> implement any loading behavior you think is suitable.
>>>> Beware that class-reloading introduces all kinds of behavior that is
>>>> counter-intuitive. Multiple instances of what appears to be the same
>>>> static variable thereby also breaking singleton patterns in your code
>>>> (which are broken already, but that is a completely different matter) is
>>>> only one example.
>>>> Please start by telling us what the real problem is that you need to
>>>> solve. Going the class-loader way is probably among the worst solutions
>>>> for your problem.
>>> I wrote a gui based unit testing framework and it loads the top level
>>> test suites from a text box with there names in it (initially
>>> populated from command line)... I do not want to have to close/reopen
>>> the app when I rewrite/recompile some code under test.
>>> --Aryeh

>> Is there any need to run the tests in-process? You could spawn an
>> external java process to run the tests and have it store the test
>> results in a place where the GUI can find it.
>>
>> Gr.
>>
>> Silvio

>
> In theory no but it sounds a little kludgist and perhaps not portable
> (I don't see how it wouldn't be but once you ask the OS for anything I
> get nervious)
>


For this kind of application the ClassLoader woes I mentioned are
probably not relevant. However, you must remember that when you run such
completely generic test code in-process you may (and probably will)
compromise the state of your VM and risk bringing down the application
itself.
I would go the out-of-process way. I do not see why this would not work
in any reasonable Java Runtime env.

Regards,

Silvio
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      10-02-2007
Aryeh M. Friedman wrote:
> On Oct 2, 7:56 am, Silvio Bierman <(E-Mail Removed)> wrote:
>> Aryeh M. Friedman wrote:
>>> On Oct 2, 6:02 am, Daniel Pitts <(E-Mail Removed)> wrote:
>>>> Right, a ClassLoader will not re-load a class. You will have to
>>>> instantiate a new class loader to do so.
>>>> ClassLoader.loadClass will first look for already loaded classes. It
>>>> will not re-load the class into the JVM.
>>>> Its generally difficult to get dynamic class behavior from Java. You
>>>> aren't able to unload a class, and load a different version of it.
>>>> Also, and already loaded classes that refer to that other class will
>>>> only be able to refer to one instance of it, not one from one class
>>>> loader, and then another from another class loader.
>>> Since I am relativally naive with class loaders how do I create a new
>>> instance of the system class loader?

>> You don't. You can write your own class loaders if you want and can
>> implement any loading behavior you think is suitable.

>
> I wrote a gui based unit testing framework and it loads the top level
> test suites from a text box with there names in it (initially
> populated from command line)... I do not want to have to close/reopen
> the app when I rewrite/recompile some code under test.


Try look at the super simple example attached below.

Arne

=============================================

import java.io.*;
import java.net.*;

public class DoubleDynmaic {
private static void dynno(int n) {
(new File("test")).mkdir();
try {
OutputStream os = new FileOutputStream("test/Test.java");
PrintStream ps = new PrintStream(os);
ps.println("public class Test {");
ps.println(" public Test() {");
ps.println(" System.out.println(" + n + ");");
ps.println(" }");
ps.println("}");
ps.close();
os.close();
Runtime.getRuntime().exec("javac -d test
test/Test.java").waitFor();
URL[] url = new URL[1];
url[0] = new URL("file:test/");
URLClassLoader cl = new URLClassLoader(url);
Class.forName("Test", true, cl).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
dynno(i);
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
KPIC makes programs running crash when libraries are recompiled jortizclaver C Programming 2 02-22-2006 10:06 PM
KPIC makes programs running crash when libraries are recompiled jortizclaver C++ 0 02-22-2006 12:12 PM
Urgent: jsp is not recompiled by Tomcat Erwin Moller Java 6 05-27-2004 07:50 PM
Asp.net page not recompiled =?Utf-8?B?QXNwLm5ldCBwYWdlIG5vdCByZWNvbXBpbGVk?= ASP .Net 1 01-31-2004 02:43 AM
Custom ClassLoader - changing order of loading to look in my loaderfirst amw Java 1 09-25-2003 03:16 AM



Advertisments