How do I discover classes at runtime in the classpath which implements a defined interface?
ServiceLoader suits well (I think, I haven’t used it), but I need do it in Java 1.5.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s nothing built into Java 1.5 for this. I implemented it myself; it’s not too complicated. However, when we upgrade to Java 6, I will have to replace calls to my implementation with calls to
ServiceLoader. I could have defined a little bridge between the app and the loader, but I only use it in a few places, and the wrapper itself would be a good candidate for a ServiceLoader.This is the core idea:
Better exception handling is left as an exercise for the reader. Also, the method could be parameterized to accept a ClassLoader of the caller’s choosing.