3

I have a project with the following structure:

+---main
|   \pom.xml
+---module1
|   \pom.xml
+---module2
|   \pom.xml
+---module3
|   \pom.xml
+---module4
|   \pom.xml
+---pom.xml

The modules uses ServiceLoader mechanism to register their services. The main class is in main module which is also responsible for loading the services in classpath using ServiceLoader mechanism. The problem is that the main module is not aware of module1, module2, module3 and module4 because they're installed by the user independently. What I want to to is to add all the classes in these modules to classpath when starting the program so that ServiceLoader will be aware of the services in these modules.

Currently, I'm loading these modules at runtime from main module but it doesn't seem to be good way of solving this problem because the IDE doesn't aware of these modules and it seems that it takes much more time to load these modules at runtime. I have looked at how modular Java projects like Elasticsearch and PrestoDB handle this problem but it seems they both load the installed modules at runtime.

How do you handle this problem when developing modular Java applications? Let's say I have a directory called plugins that has the jar files of the modules the user installed, is there any way to start the application with the classpath that contains all the jars in that directory using a maven plugin? Or do I have to load these jars at runtime?

Boyolame
  • 329
  • 2
  • 13
  • This may sound too simple, in which case I haven't understood your question correctly: If the jar-files / classes exist when you run your application, what about simply adding your folder to the classpath (-cp folder/*)? – SirRichie May 11 '15 at 15:04

1 Answers1

0

Usually the user will start the application from a script (or in windows with some configurable java exe launcher). In the script you can simply add the plugin folder to your classpath in a way similar to this:

java -cp plugin/* -jar app.jar
Fabio Bonfante
  • 5,128
  • 1
  • 32
  • 37