Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Glenn Duncan   on Feb 11 In Java Category.

  
Question Answered By: Adal Fischer   on Feb 11

A note on using runtime loaded dynamical code in Java.

No, as the previous posters have pointed out, DLL's does not work well with
Java. But since I am guessing that you still want dynamical loading in your
Java program atleast this can be achieved.

My favorite way of doing this (maybe there are others) is to work with an
interface (or base class) some properties file (haven't really seen the need
for LDAP/Contexts but they would do the trick as well) and Class.forName().

If the above terms doesnt make sense to you look them up at
http://java.sun.com

The core of my dynamic class loading would then look something like this:

String className = System.getProperty("mySystem.mySomething.className");
obj = (MySomething)( Class.forName( className ).newInstance());

Of course I could get my clas name from some other place than the system
properties (like a properties file). Also the best place to place this code
would be in a factory:

class MyFactory {
MySomething obj = null;

public synchornized MySomething getMySomething () {
if ( obj == null ) {
String className = ...;
obj = (MySomething)...;
}
return obj;
}
}

By using this factory implementation you make the creation of the dynamic
object only once... but the cost is that the creation method has to be
synchronized. You may also wish to make the factory static.

If you want to know more about factories look it up at
http://java.sun.com/blueprints/patterns/index.html (or any other online - or
book - resource on patterns).

I hope this answers some of the questions on dynamic loading in Java.

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on dll with java Or get search suggestion and latest updates.


Tagged: