问题描述
我想创建一个包含第3方jar的Java 9运行时映像.我已经制作了一个简单的Java项目(将其称为Example
)来调用实用程序jar(将其称为ExampleUtil.jar
). Example
在src
目录中包含module-info.java
,并且在Eclipse中运行良好(我已经添加了ExampleUtil.jar
作为模块依赖项).如果我打电话:
I'd like to create a Java 9 runtime image that contains 3rd party jars. I have made a simple Java project (let's call this Example
) to call a utility jar (let's call this ExampleUtil.jar
). Example
contains the module-info.java
in the src
directory and runs fine in Eclipse (I had added ExampleUtil.jar
as a module dependency). If I call:
jlink -v
--module-path "C:\Program Files\Java\jdk-9.0.4\jmods";C:\Temp
--add-modules com.example.steven
--output C:\Temp\image.steven
--launcher launch=com.example.steven/com.example.steven`
...我收到错误消息:
... I get the error message:
是否有一种使用不是模块的jar创建运行时映像的方法?谢谢.
Is there a way to create a runtime image using jars that aren't modules? Thank you.
推荐答案
不,jlink要求所有包含的模块都是 explicit ,这意味着它们需要具有模块描述符.这是文档讲述jlink的模块路径:
No, jlink requires all included modules to be explicit, meaning they need to have a module descriptor. Here's what the documentation says about the jlink's module path:
请注意,没有普通JAR"(即没有描述符的JAR).
Note the absence of "plain JARs" (i.e. JARs without descriptor).
尽管如此,您可以将现有的第三方JAR升级为模块化JAR.这些步骤是:
You can upgrade existing third-party JARs to modular JARs, though (with some effort). The steps are:
- 获取JAR及其所有依赖项
- 为此创建一个
module-info.java
(手动或使用JDeps) - 使用
--patch-module
将其编译为module-info.class
,以告知编译器有关源代码 - 使用
jar --update
将模块声明添加到现有的JAR
- get the JAR and all its dependencies
- create a
module-info.java
for it (either manually or with JDeps) - compile it to
module-info.class
with--patch-module
to tell the compiler about the sources - use
jar --update
to add the module declaration to the existing JAR
或者,您可以使用类似 ModiTect 的工具来为您执行这些操作.
Alternatively, you can use a tool like ModiTect that does these things for you.
这篇关于Java 9:使用第3方jars通过JLink生成运行时映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!