如何将JAXB运行时库与OpenJDK 11捆绑



背景

在我的组织中,有很多基于Java的Windows桌面应用程序。在我们的专有软件部署系统中,应用程序不包含单独的JRE/JDK。而是将常见的JRE(到目前为止的Oracle 8 Java SE JRE)部署到用户机器,该机器基于环境变量将其连接到单个应用程序。

我们希望将JRE从Oracle 8 Java SE JRE迁移到OpenJDK 11,而无需修改应用程序。从Java 8到Java 11,各种库被删除,e。G。Javafx。使用Java模块系统和JLINK工具,我可以使用OpenJFX来创建自己的专有JDK:

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

需要Javafx的应用程序可以使用此专有JDK而不会更改。

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

jlink --module-path ..mods;C:Tempjaxb-ri-2.4.0-b180830.0438jaxb-rimod --add-modules=java.xml.bind,java.base,java.compiler,java.datatransfer,java.desktop,java.instrument,... --output C:WorkspaceJavaOpenJDK_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用作其依赖性,并被迫将其用作自动模块或从class Pather使用缺少模块info.java描述符文件。"

根据可以在JDK 11上使用JLink进行运行时,包括已删除的Java SE EE模块,可以使用JLINK?我可以使用Java 10中的Jlink捆绑模块Java.xml.bind,但仍包含它。确实,Jlink成功了,如果我将两个模块从OpenJDK 10.0.2添加到模块路径:

jlink --module-path ..mods;C:Tempjdk-10.0.2jmodsjava.xml.bind.jmod;C:Tempjdk-10.0.2jmodsjava.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捆绑在一起?

提示

  • 我知道如何在构建经典的Java应用程序构建的构建时间解决方法:如何解决Java.lang.noclassdeffounderror:Javax/XML/bind/bind/jaxbexception在Java 9中或使用Maven包括JAXB。但是,如果我不想触摸已部署的应用程序二进制文件,则在这种情况下构建时间解决方案无济于事。

尝试包括这两个依赖性:

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

如果您使用的是javax API模块,则通常不可能在不修改应用程序的情况下从JDK8升级。自JDK9以来实际被删除的com.sun软件包等一些实现,您必须将它们添加为依赖项。

您还陷入了java.lang.NoSuchMethodException,因此当前可用实现与JDK10

javax API不再兼容

我的组织最终屈服于在编译时修改应用程序。这不是我正在搜索的解决方案,但至少对我们有用。

jaxb已从版本11开始从JDK中删除,您需要在添加所有其他依赖项时添加它。

Maven的示例:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>${jaxb.api.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>${jaxb.api.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>${jaxb.api.version}</version>
</dependency>

相关内容

  • 没有找到相关文章

最新更新