如何在Java9模块中使用第三方库? [英] How to use 3rd party library in Java9 module?
问题描述
我有一些java9模块,它使用的第三方库不是Java9模块,只是一个简单的实用程序jar。
I have some java9 module that uses 3rd party library that is not Java9 module, just a simple utility jar.
然而,编译器抱怨它不能从我的实用程序中找到一个包。
However, the compiler complains that it can't find a package from my utility.
我应该怎么做 module-info.java
来启用我的用法第三方库?
What should I do in module-info.java
to enable usage of my 3rd party library?
推荐答案
您可以将库用作自动模块。自动模块是没有模块描述符的模块(即 module-info.class
)。但是你应该使用什么名称来引用这个模块?自动模块的名称源自JAR名称(除非此JAR包含自动模块名称
属性)。完整规则很长(请参阅Javadoc for ModuleFinder.of
),因此为简单起见,您只需从其名称中删除该版本,然后替换所有非字母数字字符带点(。
)。
You can use your library as an automatic module. An automatic module is a module that doesn't have a module descriptor (i.e. module-info.class
). But what name should you use to refer to this module? The name of the automatic module is derived from the JAR name (unless this JAR contains an Automatic-Module-Name
attribute). The full rule is quite long (see Javadoc for ModuleFinder.of
), so for simplicity, you just have to drop the version from its name and then replace all non-alphanumeric characters with dots (.
).
例如,如果你想使用 foo- bar-1.2.3-SNAPSHOT.jar
,您需要将以下行添加到 module-info.java
:
For example, if you want to use foo-bar-1.2.3-SNAPSHOT.jar
, you need to add the following line to module-info.java
:
module <name> {
requires foo.bar;
}
这篇关于如何在Java9模块中使用第三方库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!