views:

52

answers:

3

Hello,

I am creating a java client using different libraries, they are the same base, but with different optimizations.

Is there a pattern (or something else) that I can use to load different libraries, e.g.:

java -jar myapp.jar 1 (loads with libraries from set 1 and imports correct and creates a client using this library)....

Update: I have tried OSGI and it seems like it loads jars as services. I need a way to load very similar libraries upon startup an app. The library has exactly the same package/method names, but differ in some calculation. I need to have the exact same client to get realistic results. Thanks

A: 

It sounds a little like the java.sql interface idea might work. Write interfaces that your clients use, then create an implementation JAR that is loaded according to a particular parameter like JDBC drivers.

duffymo
Thanks for quick reply, but I am new to java, could you please give me a link or example to this?
Xiang Hoa
A: 

This sounds like a good use case for OSGI. Here's some good tutorials. Each of your libraries would be a separate jar file. OSGI lets you load or unload each jar as you desire. You can swap libraries out on the fly without restarting your app in fact.

Edit:

Here is a good example app.

Jay Askren
I read the tutorials, but it seems to me that OSGI is used in enterprise apps? thus running as servlets or in web start?
Xiang Hoa
OSGI can be run with Java apps in general. It doesn't have to be an enterprise app.
Jay Askren
I will look into this. Thanks, will come back once I have tested it. Thanks
Xiang Hoa
Thanks for all help. Felix using Service-Based Application embedding the Framework, seems to give me what I needed.Thanks!
Xiang Hoa
Seems like I was too quick...Is it correct that if I want to load different libraries/jar, I must create a copy of the client and the load the client as a service in the host?
Xiang Hoa
A: 

As an alternative to OSGi (which is a good way to do it) you can use Java Web Start., if your dependencies are statical one (that's to say if you won't change the library version during runtime).

If you're in that case, it's as simple as having the JNLP file generated dynamically, with the libraries version chosen according to user parameters.

Riduidel
This is a small client for testing overlay/routing protocol.I have managed to create a jar file with dependency to a overlay library. I would like to have a way to change the library based on startup parameter when running the jar file. Unsure if I can use Web Start, but I guess as less as possible would be preferred.
Xiang Hoa