我可以通过MANIFEST.MF要求Java 9模块吗? [英] Can I require Java 9 module via MANIFEST.MF?

查看:65
本文介绍了我可以通过MANIFEST.MF要求Java 9模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java库应该与Java 8和Java 9兼容.要与Java 9一起运行,我们需要一些Java 9模块.

My Java library should be compatible with Java 8 and Java 9. For running with Java 9 we need some of the Java 9 modules.

我知道我可以使用--add-modules通过命令行添加它.但这是一个库,我无法控制命令行.

I know that I can add it via command line with --add-modules. But it is a library and I can't control the command line.

MANIFEST.MF中是否有与--add-modules相等效的内容?还是有其他与Java 8兼容的解决方案?

Is there an equivalent of --add-modules in MANIFEST.MF? Or is there any other solution that is compatible with Java 8?

推荐答案

不幸的是,MANIFEST.MF中没有--add-modules的等价物.但是,您可以创建module-info.java并在其中声明依赖项:

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>;
    ...
}

但是,如果您编译module-info.java并只是将module-info.class放入JAR,则在某些平台(例如Android)上可能无法使用.那么该怎么办? Java 9中有一个新功能:多发行版JAR文件( JEP 238 ).这个想法是,您可以将Java 9类文件放在一个特殊目录(META-INF/version/9/)中,并且Java 9将正确处理它们(而Java 8将忽略它们).

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:

  • javac --release 8
  • 编译除module-info.java以外的所有类
  • javac --release 9编译module-info.java.
  • 构建一个JAR文件,使其具有以下结构:
  • 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

生成的JAR应该与Java 8和Java 9兼容.

The resulting JAR should be compatible with Java 8 and Java 9.

此外,您只需将module-info.class放入META-INF文件夹.这将具有相同的效果.但是,这可能不适用于某些工具和平台.所以我认为第一种方法更可取.

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.

这篇关于我可以通过MANIFEST.MF要求Java 9模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆