如何将 JAXB 运行时库与 OpenJDK 11 捆绑在一起?

2022-09-02 14:12:29

背景

在我的组织中,有很多基于Java的Windows桌面应用程序。在我们的专有软件部署系统中,应用程序不包含单独的JRE / JDK。相反,一个通用的JRE(到目前为止Oracle 8 Java SE JRE)被部署到用户机器上,该机器根据环境变量附加到各个应用程序。

我们希望将 JRE 从 Oracle 8 Java SE JRE 迁移到 OpenJDK 11,而无需修改应用程序。从Java 8到Java 11,各种库都被删除了,例如JavaFX。使用Java模块系统和jlink工具,我可以创建自己的专有JDK,并用OpenJFX增强:

jlink --module-path ..\mods;C:\Temp\javafx-jmods-11.0.2 --add-modules=ALL-MODULE-PATH,java.base,java.compiler,java.datatransfer,java.desktop,... --output C:\Temp\OpenJDK+OpenJFX

需要JavaFX的应用程序可以使用这个专有的JDK而不被改变。

当我尝试将相同的方法应用于JAXB的最新测试版时,我收到以下错误:

jlink --module-path ..\mods;C:\Temp\jaxb-ri-2.4.0-b180830.0438\jaxb-ri\mod --add-modules=java.xml.bind,java.base,java.compiler,java.datatransfer,java.desktop,java.instrument,... --output C:\Workspace\Java\OpenJDK_11.0.2_JFX_JAXB
Error: automatic module cannot be used with jlink: java.activation from file:///C:/Temp/jaxb-ri-2.4.0-b180830.0438/jaxb-ri/mod/javax.activation-api.jar

根据 https://github.com/eclipse-ee4j/jaf/issues/13,“JAXB API使用 activation-api作为其依赖项,并且由于缺少模块信息.java描述符文件而被迫将其用作自动模块或从类路径使用。"

此外,是否可以在JDK 11上使用jlink来创建一个运行时,包括被删除的Java SE EE模块?我可以使用Java 10中的jlink捆绑java.xml.bind模块,它仍然包含它。事实上,如果我将两个模块从 OpenJDK 10.0.2 添加到模块路径中,jlink 会成功:

jlink --module-path ..\mods;C:\Temp\jdk-10.0.2\jmods\java.xml.bind.jmod;C:\Temp\jdk-10.0.2\jmods\java.activation.jmod --add-modules=java.xml.bind, ...

但是当我使用生成的JDK运行我的应用程序时,我得到这样的错误:

!ENTRY org.eclipse.osgi 4 0 2019-02-15 15:58:58.642
!MESSAGE Application error
!STACK 1
org.eclipse.e4.core.di.InjectionException: java.lang.NoClassDefFoundError: javax/annotation/PostConstruct
    at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:410)
    at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:318)
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:162)
    at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createDefaultHeadlessContext(E4Application.java:491)
    at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createDefaultContext(E4Application.java:505)
    at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createE4Workbench(E4Application.java:204)
    at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:614)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:151)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
Caused by: java.lang.NoClassDefFoundError: javax/annotation/PostConstruct
    at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:124)
    at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:399)
    ... 22 more
Caused by: java.lang.ClassNotFoundException: javax.annotation.PostConstruct cannot be found by org.eclipse.e4.core.di_1.6.100.v20170421-1418
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:433)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:395)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:387)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:150)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 24 more

或者这个:

Feb. 15, 2019 2:49:03 NACHM. com.sun.xml.internal.bind.v2.runtime.reflect.opt.Injector <clinit>
 SEVERE: null
 java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String, [B, int, int, java.lang.ClassLoader, java.security.ProtectionDomain)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.reflect.opt.Injector.<clinit>(Injector.java:182)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:66)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:164)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:270)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.property.ArrayProperty.<init>(ArrayProperty.java:53)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.<init>(ArrayERProperty.java:73)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:85)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.<init>(ArrayElementNodeProperty.java:47)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:499)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:316)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1141)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:150)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.xml.bind@10.0.2/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:282)
    at java.xml.bind@10.0.2/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:271)
    at java.xml.bind@10.0.2/javax.xml.bind.ContextFinder.find(ContextFinder.java:406)
    at java.xml.bind@10.0.2/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:706)
    at java.xml.bind@10.0.2/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:647)
    at com.xxx.build.automation.utils.internal.CategoryFileHandler.unMarshalFile(CategoryFileHandler.java:17)
    at com.xxx.build.automation.utils.common.GetComponentName.doExecuteInternal(GetComponentName.java:89)
    at com.xxx.build.automation.utils.common.GetComponentName.execute(GetComponentName.java:72)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:357)
    at org.apache.tools.ant.Target.performTasks(Target.java:385)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
    at org.apache.tools.ant.Main.runBuild(Main.java:758)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
 Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String, [B, int, int, java.lang.ClassLoader, java.security.ProtectionDomain)
    at java.base/java.lang.Class.getMethod(Class.java:2109)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:186)
    at java.xml.bind@10.0.2/com.sun.xml.internal.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:182)
    ... 48 more

问题

有没有可能,我怎么能把 JAXB 库与 OpenJDK 11 捆绑在一起?

提示


答案 1

尝试包含以下两个依赖项:

    implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
    implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime'

答案 2

如果您使用的是 API 模块,通常无法在不修改应用程序的情况下从 jdk8 升级。有些实现喜欢的包,从jdk9开始就被物理删除了,你必须将它们添加为依赖项。javaxcom.sun

您还遇到了,因此当前可用的实现不再与jdk10中的API兼容java.lang.NoSuchMethodExceptionjavax


推荐