Reputation: 527
I am writing a Java application that will load third party "drivers". I intend to develop an interface that will define the required methods that the "driver" must implement in order to work within the application.
The intent is for the "driver" to be contained within a jar file. Then third party "driver" developers will be required to implement the interface.
As I'm trying to figure this out, I moved the "driver" code to a jar and found that I am having trouble since I have the interface defined in both the main application and in the jar file.
Is there a recommended way to implement this approach, or am I headed the wrong way?
j
Upvotes: 2
Views: 1209
Reputation: 56129
Since the interface is part of the API / library you're providing, you should have the interface there and the third parties should import your API (from your jar) and implement the appropriate interface, like how the Java API provides a SQL interface, but not an actual implementation, and MySQL implements the Java API interface. If your interface for the driver has use outside your application, you could pack it as a separate jar, but if it's really only useful for interaction with your library, it should be packed with the rest of the library.
Upvotes: 1
Reputation: 5005
If you'e actually moved the interface you need to rebuild both jars. If you've copied it then you'll need to give one a different package name
Upvotes: -1
Reputation: 88737
You basically define the interface once, e.g. in an API jar. This jar is then used by the main application as well as the driver.
The driver library would then have this API jar as a dependency that is provided by the main application.
In terms of Maven the dependency would have scope provided
or compile
for the library.
Note that you should avoid having the same class twice in different libraries since those will most likely lead to class loading problems, class cast exceptions etc. (unless there's some class loader scoping that isolates applications, like most application servers do).
Upvotes: 6