【问题标题】:Java 9 dependency issues regarding serviceloader关于 serviceloader 的 Java 9 依赖问题
【发布时间】:2017-11-22 14:58:47
【问题描述】:

我有一个关于 Java 9 中服务加载器如何根据这种情况发生变化的问题


场景

项目gert类Main

package gert;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Main { 
    static public void test() throws JAXBException {
        InputStream is = new ByteArrayInputStream("<Classes RUNTIME_INCLUDE_JARS=\"\"><Class></Class></Classes>".getBytes(StandardCharsets.UTF_8)); 
        JAXBContext jaxbContext = JAXBContext.newInstance(ClassesDef.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.unmarshal(is);
    }
}

项目gert类ClassesDef

package gert;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Classes")
public class ClassesDef {
    @XmlAttribute(name="RUNTIME_INCLUDE_JARS")
    public String jars=null;

    @XmlElement(name="Class")
    public String classes;
}

项目gertpom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>gert</groupId>
    <artifactId>gert</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>gert.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency><!-- org.eclipse.persistence.jaxb.JAXBContextFactory comes with this dependency-->
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.26</version>
        </dependency>
    </dependencies>
</project>

项目cristina类Main

package cristina;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
        String bWithDep = "C:\\Users\\gert\\eclipse-workspace91java\\gert\\target\\gert-0.0.1-SNAPSHOT-jar-with-dependencies.jar";
        List<URL> jars = new java.util.ArrayList<URL>();
        File f;
        f = new File(bWithDep);
        jars.add(f.toURL());
        URL[] urls = (URL[])jars.toArray(new URL[jars.size()]);
        URLClassLoader urlloader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader());
        System.out.println("Before\tgert.Main.test();.");
        Class<?> c= Class.forName("gert.Main", true, urlloader);
        Object gert = c.newInstance();
        Method m = c.getMethod("test");
        m.invoke(gert);
        System.out.println("After\tgert.Main.test();.");
    }
}

项目cristinapom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cristina</groupId>
  <artifactId>cristina</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <!-- <dependency>
      <groupId>javax.xml.bind</groupId>
         <artifactId>jaxb-api</artifactId>
         <version>2.3.0</version>
      </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.26</version>
    </dependency> -->
  </dependencies>
</project>

所以cristinamain加载了gert项目并执行了一个名为test()的gert方法

测试

Java 8
当项目使用 java 8 运行时,它可以工作

命令

"C:\Program Files\Java\jre1.8.0_151\bin\java.exe" -cp C:\Users\gert\eclipse-workspace91java\cristina\target\cristina-0.0.1-SNAPSHOT.jar cristina.Main

输出

Before gert.Main.test();. After gert.Main.test();.
Java 9
当使用 java 9 完成相同操作时,它不会

命令

"C:\Program Files\Java\jre-9.0.1\bin\java.exe" -cp C:\Users\gert\eclipse-workspace91java\cristina\target\cristina-0.0.1-SNAPSHOT.jar cristina.Main

输出

Before  gert.Main.test();.
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at cristina.Main.main(Main.java:23)
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:397)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
        at gert.Main.test(Main.java:14)
        ... 5 more
Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
        at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
        ... 9 more

以上 2 是直接从命令行完成的。
但是
当我取消注释 cristina 项目的 pom.xml 中的依赖项并执行 maven 安装并 从 eclipse 运行项目 时,java 9 工作。所以看来eclipse在运行项目的时候也考虑到了maven依赖。

问题

当依赖项位于 gert 项目中并且仅由 gert 项目使用时,为什么 cristina 项目在使用 Java 9 运行时会引发异常?

【问题讨论】:

  • 如果我没看错,那么您已经在 cristina-0.0.1-SNAPSHOT.jar 中获得了 JAXB 2.3.0 API 和 Eclipse 实现,对吗?我不知道这是否相关,但是当您使用 JDK 8 时,您使用的是 JDK 中包含的 JAXB 2.2。当您使用 JDK 9 时,我假设它正在从您的 JAR 文件中加载 JAXB 2.3.0 API 类。
  • @AlanBateman 添加有关 maven 行为的详细信息,jar gert-0.0.1-SNAPSHOT-jar-with-dependencies.jar` 具有 jaxb-api:2.3.0 和 jersey-media-moxy:2.26,在cristina 项目被注释掉。
  • 好的,尽管我假设您明白我的观点,即在 JDK 8 上运行时,不会从该 JAR 文件中加载 JAXB 类。因此,该示例是从 JAXB 2.2 升级到 2.3.0,也许有那里有一个问题。

标签: java reflection dependencies java-9 serviceloader


【解决方案1】:

异常的原因可能是java.xml.bind 是一个可升级的模块。

正如 JEP 261: Module System 在模块的风险和假设中所述:

如果在命名模块和类路径中都定义了包 那么类路径上的包将被忽略。类路径 因此,不能再用于扩充已构建的包 进入环境。

因此,org.eclipse.persistence.jaxb 包似乎被忽略了。最终,ContextFinder.newInstance 行代码调用 JAXBContextFactory.createContext 并提供要绑定的类 (ClassDef)。文档进一步指出

抛出JAXBException - 如果在创建JAXBContext 时遇到错误,例如(但不限于):...

  • classesToBeBound 未对java.xml.bind 模块开放

您可以尝试在运行应用程序时使用

--upgrade-module-path /path/to/jaxb-api/dependency...

A : 分隔的目录列表, 每个目录都是一个模块目录 替换运行时映像中的可升级模块

【讨论】:

  • 这个升级模块路径应该指向jersey-media-moxy依赖的jar吗?如果是这样,这是行不通的。 "C:\Program Files\Java\jre-9.0.1\bin\java.exe" -cp C:\Users\gert\eclipse-workspace91java\cristina\target\cristina-0.0.1-SNAPSHOT.jar cristina.Main --upgrade-module-path C:\Users\gert\.m2\repository\org\glassfish\jersey\media\jersey-media-moxy\2.26\jersey-media-moxy-2.26.jar
  • @GertKommer 我认为另一个 jar 应该负责 jaxb-api,`C:\Users\gert\.m2\repository\javax\xml\bind\2.3.0`
  • 感谢您将我指向升级模块路径:) 我现在拥有.\java.exe --upgrade-module-path "C:\Users\gert\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.moxy\2.7.0;C:\Users\gert\.m2\repository\javax\xml\bind\jaxb-api\2.3.0;C:\Users\gert\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.core\2.7.0" --add-modules org.eclipse.persistence.moxy,org.eclipse.persistence.core,java.xml.bind -cp C:\Users\gert\eclipse-workspace91java\cristina\target\cristina-0.0.1-SNAPSHOT.jar cristina.Main,这在这种特殊情况下对我有用。
猜你喜欢
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多