Unfortunately, there is no equivalent of --add-modules
in MANIFEST.MF
. However, you can create module-info.java
and declare your dependency there:
module <module-name> {
requires <dependency>;
...
}
However, if you compile module-info.java
and simply put module-info.class
to your JAR, it may not work on some platforms (e.g. Android). So what to do? There is a new feature in Java 9: multi-release JAR files (JEP 238). The idea is that you can put Java 9 class files to a special directory (META-INF/version/9/
) and Java 9 will properly handle them (while Java 8 will ignore them).
So, these are the steps that you should perform:
- Compile all classes except
module-info.java
with javac --release
8
- Compile
module-info.java
with javac --release 9
.
- Build a JAR file so it will have the following structure:
JAR root
- A.class
- B.class
- C.class
...
- META-INF
- versions
- 9
- module-info.class
The resulting JAR should be compatible with Java 8 and Java 9.
Also, you can just put module-info.class
to the META-INF
folder. This will have the same effect. This, however, may not work for some tools and platforms. So I think the first way is more preferable.