The Artima Developer Community
Sponsored Link

Java Answers Forum
Dynamic class loading

6 replies on 1 page. Most recent reply: Jul 31, 2003 2:53 PM by Hiran

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Dynamic class loading Posted: Jul 30, 2003 11:04 AM
Reply to this message Reply
Advertisement
Is it possible to dynamically load/use classes (modules) in Java? If so, how do I do that? Thanks for the help.

God bless
Hiran


Mr Plow

Posts: 18
Nickname: mrplow
Registered: Jun, 2003

Re: Dynamic class loading Posted: Jul 30, 2003 12:33 PM
Reply to this message Reply
By default, Java loads classes dynamically. A class is NOT loaded into memory until the Java program either needs:
1) An object of that class
2) Access to a static member of that class

Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Re: Dynamic class loading Posted: Jul 30, 2003 12:38 PM
Reply to this message Reply
I'm writing a modular application though that allows the user to dynamically load/unload modules. How should I do this without knowing ahead of time the name of the class? I could always just have each module use a config file specifying the name and location of the module's main class, but I still would only find out the name after I read the config file. So how should I dynamically load each module? Thanks.

God bless
Hiran

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Dynamic class loading Posted: Jul 31, 2003 6:20 AM
Reply to this message Reply
I'm a bit confused as to what it is you are actually trying to achieve and what problem it is that you are actually trying to solve, but it sounds to me like you should be writing your own class loader (java.lang.ClassLoader).

Greg

Posts: 18
Nickname: singleton
Registered: Jun, 2003

Re: Dynamic class loading Posted: Jul 31, 2003 8:52 AM
Reply to this message Reply
Use Class.forName(String s) to load a class based
on an unknown (at design time) class name.
Usually this approach involves the use of an interface
that all of the expected object types implement. Otherwise
you will only have access to the methods of the Object
class to use for the Object created with the forName method.

Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: Dynamic class loading Posted: Jul 31, 2003 9:18 AM
Reply to this message Reply
If you change a class while the program is running and you use Class.forName to load it, it will only load the new version of the class if you didn't load the class before. If you did, the app. needs to be restarted to see the new class.

The other solution is indeed writing your own ClassLoader, that does check for changed classes and is capable of reloading them.

Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Re: Dynamic class loading Posted: Jul 31, 2003 2:53 PM
Reply to this message Reply
Well, the classes are going to be modules that will run in their own threads. I'm planning to have a main engine that load/unloads the modules and facilitates intermodule communication. Thus, the modules can't really be based on a certain interface. What if I just had the modules implement Runnable? That way all I would need to do is have the main engine call the start method of each class.

Also, even if that's going to be the case, how exactly would I load each module? Could someone please give a basic code example, if you don't mind (since I've never played around with using ClassLoader before). I'll probably have a config file that specifies the names and paths of the classes to load. Thanks.

God bless
Hiran

Flat View: This topic has 6 replies on 1 page
Topic: Decimal Numbers Manipulation Previous Topic   Next Topic Topic: Programming Exercises Solution/Answers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use