views:

203

answers:

2

I am developing a library. I want some functionality to be additionally available in case a certain other library is in the classpath. My understanding is:

  • there would be a buildtime dependency, since I import from that lib and use it in places
  • runtime dependency is there only if the code path reaches the point where I am using something from that library

Am I correct?

+6  A: 

Am I correct?

Yes.

runtime dependency only arises if the code path reaches ...

I would it name execution path. At any time the code in execution will reach some

a.A a = new a.A();

And if class a.A is not on your classpath it will throw a Runntime Exception. That mean you have to run and reach this point to get an error. If your program don't reach this point it never fails.

PeterMmm
+2  A: 

You may already be doing this, but in cases where the class may or may not be on the classpath at runtime I'd recommend using ClassLoader to explicitly load the class, taking appropriate action if the class is not found (as this doesn't sound like an error condition in your case). It'll make the code a lot clearer to read rather than having something like:

try {
  new MyClass(); // Could potentially thrown a ClassNotFoundException.
  // ... yada yada
} catch(ClassNotFoundException ex) {
  // Do something else instead.
}

Other approach you might want to consider is wrapping your library calls in an adapter layer and providing a No-Op implementation of the adapter in cases where the library isn't available, making the rest of your code agnostic to whether the library is present or not.

Adamski