Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

Swappable class / jar?

Thread view: 
Kevin - 08 Apr 2008 22:34 GMT
Do not know if this is possible or not:

Can we "hot swap" the implementation of a class at run time, without
restart the program?

Such as:

1) Java program A is running all the time.
2) A calls a particular class B.
3) At run time, A can choose which implementation of B to use. The
exact implementation may not be known in advance.
For example, if the developer of B found a better way to code B, he
can rewrite B and put B in a separate jar file. And program A can
simply load that jar file and use the new code for B.
(We can assume different implementation of B use the same interface or
base class, so the calling part in A does not need to change).

Does this make sense?
Arne Vajhøj - 08 Apr 2008 23:59 GMT
> Do not know if this is possible or not:
>
[quoted text clipped - 14 lines]
>
> Does this make sense?

Load B with a special classloader.

When you want a new B just replace the classloader.

Arne
Mike  Schilling - 09 Apr 2008 00:13 GMT
> Do not know if this is possible or not:
>
[quoted text clipped - 14 lines]
>
> Does this make sense?

In a sense, this is how Java works all the time.  When it's time to load B,
the JVM loads the version of B that's currently available.  So long as it's
compatible with the B that was present at compilation time [1], there won't
be any problems.  What I'm not sure about is what you mean by:

   3) At run time, A can choose which implementation of B to use

How will A make this decision?

By the way, a more common pattern for choosing an implementation at run-time
is for B to be an interface rather than a concrete class.  At run-time a
particular implementation of the interface can be loaded; since all A knows
about the class is that it implements B, everything works normally.

1. The Java Langauge Speciftication lists what "compatible" means in gory
detail, but, basically if all of the original fields and methods  are still
present with the same names and types, you're OK.
Kevin - 09 Apr 2008 00:29 GMT
On Apr 8, 4:13 pm, "Mike  Schilling" <mscottschill...@hotmail.com>
wrote:

> > Do not know if this is possible or not:
>
[quoted text clipped - 32 lines]
> detail, but, basically if all of the original fields and methods  are still
> present with the same names and types, you're OK.

What I mean is:

Say A is a GUI application, it has a open file browser button. The
user can use the open file browser to locate a .jar file, which
contains the code for B (say, Tom has his one Tom_B.jar, Kevin has his
Kevin_B.jar, etc). And all these Tom_B.jar, Kevin_B.jar etc. have the
same functions (interface). Then A can call this interface to do what
it wants to do.

These Tom_B.jar, Kevin_B.jar etc are not known in advance. We only
know they all follow the same interfaces and have the same function
names.

A good example I can think of is that the class B implement the sort
function. So it is left out side A so that different people can write
them own sort, which take the same input and have the same output
format, also same function name etc (interface). But different people
can code it using different algorithms. And application A does not
know that, also does not care it. All it does is at run time after it
starts up, "hot swap" the sort from whatever .jar file the user
provides. These .jar file are not known in advance.
Mark Space - 09 Apr 2008 01:12 GMT
> What I mean is:
>
> Say A is a GUI application, it has a open file browser button. The
> user can use the open file browser to locate a .jar file, which
> contains the code for B (say, Tom has his one Tom_B.jar, Kevin has his
> Kevin_B.jar, etc). And all these Tom_B.jar, Kevin_B.jar etc. have the

I think Arne's post covers this.  See his post above.

When the user selects a .jar file, dump your existing classloader (that
you made before) and make a new one, then load the .jar file with the
classloader.

I'm sure it will be a fair amount of work, but the outline of what to do
makes sense.
Lew - 09 Apr 2008 01:23 GMT
>> What I mean is:
>>
[quoted text clipped - 11 lines]
> I'm sure it will be a fair amount of work, but the outline of what to do
> makes sense.

Also, the debugger interface lets the JVM hotswap a class for a new (or old)
implementation without restarting the JVM.

Signature

Lew

Mike  Schilling - 09 Apr 2008 01:56 GMT
> On Apr 8, 4:13 pm, "Mike  Schilling" <mscottschill...@hotmail.com>
> wrote:
[quoted text clipped - 63 lines]
> starts up, "hot swap" the sort from whatever .jar file the user
> provides. These .jar file are not known in advance.

Then the answer Arne game is correct, but it's still easiest to have the
loaded class implement an interface (call it BInt) rather than replace a
concrete class.  When you're given the jar file, create a classloader that
reads from Kevin_B.jar and has the classloader from which A was loaded as a
parent.  You can get the class object from the new class loader, and use it
to instantiate the object, which you can cast to BInt, and then call
whatever methods you like on it. This gets more difficult if you don't know
a type that B implements (or extends); then you can only treat the instance
as an Object, and need to use reflection to make it do anything.
Roedy Green - 09 Apr 2008 00:41 GMT
On Tue, 8 Apr 2008 14:34:45 -0700 (PDT), Kevin
<happy_singapore@yahoo.com.sg> wrote, quoted or indirectly quoted
someone who said :

>Can we "hot swap" the implementation of a class at run time, without
>restart the program?

see http://mindprod.com/jgloss/classloader.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Logan Shaw - 09 Apr 2008 02:58 GMT
> Do not know if this is possible or not:
>
[quoted text clipped - 14 lines]
>
> Does this make sense?

I have never experimented with it, but OSGi seems to aim to do what
you are describing:

    http://www.osgi.org/About/Technology

It may be too heavy-duty a framework for whatever you're trying to
accomplish, though.

  - Logan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.