I imagine my application like this:
I have an application for encrypting files. And I want to extend ciphers in my application(jar) with another cipher later.
So, I will have a folder with ciphers (jar or java files) and my application will read the files from this folder. Then, in GUI, there will be a list with files(jar or java), which have a method encrypt and decrypt (I would test it with reflection). And user will choose one.
Could someone give me an advice? How make it, that it could be extended? Or how my application(.jar) could work with another .jar, .java file(read them and run them)?
Another way to do this would be the
java.util.ServiceLoadermechanism. (Read the documentation.)Your additional jar would contain a file
META-INF/services/my.package.Cipherlisting all implementations of your Cipher interface (or abstract class), and then you can sayUsing
ServiceLoadermandates that your implementations have public no-argument constructors – if they don’t have, use instead aServiceLoaderfor some factory interface.The URL should be either a
jar:URL to the jar file in which the classes are, or a non-jar URL which points to the root directory where the.classfiles are in (in their package structure).