shikhar
shikhar

Reputation: 2492

Java buildtime and runtime dependencies

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:

Am I correct?

Upvotes: 2

Views: 924

Answers (2)

Adamski
Adamski

Reputation: 54715

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.

Upvotes: 2

PeterMmm
PeterMmm

Reputation: 24630

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.

Upvotes: 6

Related Questions