Conditional Java Package Distributions

I know there isn’t such a thing as conditional compilation as in C/C++ but I wonder if it is possible to produce a java program conditionally based on requirements. For example, there could be a public version which contains some features and private version which contains more features.

Am I right to believe that the only way to achieve this is with the help of something like plugins, i.e. the different features are found dynamically if they are present in the classpath?


I would have multiple modules for your application and multiple jars. I would have all the “private” features in a jar by itself and the rest in one or more jars.

Your application would then use the features which are available in the jars distributed (there are any number of ways to do this)

A library which you might find useful is Reflections This library allows you to find all the class which implement interface or have an annotation. So you could have a Feature interface and ask it to give you all the class which are Features

Leave a Reply

Your email address will not be published. Required fields are marked *