Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reusing code in memory like DLLs

Status
Not open for further replies.

fmw1

Programmer
May 21, 2003
5
GB
I have an applet uses ClassLoaders to load sections of the application when they are needed. Many sections use identical classes to handle such things as date handling and DB access.

Is there a way in Java to reuse the same code like a DLL? Java seems to treat the same class loaded by different ClassLoaders as different code, so wants to load it again.

I could just implement all the common code in the stub applet and use Class.getMethod and Method.Invoke, but this seems a longwinded and tedious way of doing it. Is there a solution based around, say, Javabeans, where the module know about the methods and properties already?

Any help greatly appreciated.
 
>> Is there a way in Java to reuse the same code like a DLL?

Make a static method of a class then it can be called without creating an instance of the class.

>> where the module know about the methods and properties already?

Maybe you are referring to introspection. I can’t tell for sure. know about the methods [red]already[/red] That is just standard early binding yes?


-pete
 
>> Is there a way in Java to reuse the same code like a DLL?

> Make a static method of a class then it can be called without creating an instance of the class.

>> where the module know about the methods and properties already?

> Maybe you are referring to introspection. I can’t tell for sure. know about the methods already That is just standard early binding yes?

The problem here is that I have to avoid early binding (as far as I am aware). If the stub applet statically links to any of the modules, they will get loaded immediately on startup. This negates the value of loading the modules on demand.

I need a way of allowing the properties to be known by the module at compile time, but access via a different ClassLoader at run time.

Does this make sense?
 
[blue]I need a way of allowing the properties to be known by the module at compile time, but access via a different ClassLoader at run time.[/blue]

That’s what interfaces are for. Define an interface and implement it in all the classes that you want to load dynamically. Then your module can use early binding to the interface.


-pete
 
I'll try this one again then.

When I did this before, it worked fine in the JBuilder environment, but under the Sun Java Plugin, it didn't see that the interface specified under one classloader was the same as the one loaded under a different classloader.
 
You should have a mechanism for you ClassLoader to know what .jar file to load that contains the .class. Also it helps if the classes are in a package as this creates a namespace that can provide a uniqueness

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top