javax.xml.bind.JAXBException Hibernate Java 10

时间:2018-10-30 03:32:01

标签: java hibernate java-ee module java-10

我正在尝试将Hibernate Gradle项目更新为Java10。出现以下错误:

Caused by: org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
        at org.hibernate.orm.core@5.3.6.Final/org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
        at org.hibernate.orm.core@5.3.6.Final/org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
        at org.hibernate.orm.core@5.3.6.Final/org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
        at org.hibernate.orm.core@5.3.6.Final/org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:165)
        at org.hibernate.orm.core@5.3.6.Final/org.hibernate.cfg.Configuration.configure(Configuration.java:258)
        at org.hibernate.orm.core@5.3.6.Final/org.hibernate.cfg.Configuration.configure(Configuration.java:244)
        at com.kiryltkach.bettingtools.betstore.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
        ... 75 more
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: com.sun.xml.bind.v2.ContextFactory]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:409)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
        at org.hibernate.orm.core@5.3.6.Final/org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
        ... 81 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
        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)
        ... 85 more

我仔细阅读了this answer,这是我的build.gradle文件:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'com.project.example.Main'

repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.gradle:osdetector-gradle-plugin:1.6.0'
    }
}

apply plugin: 'com.google.osdetector'

ext.platform = osdetector.os == 'osx' ? 'mac' : osdetector.os == 'windows' ? 'win' : osdetector.os

compileJava {
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls'
        ]
    }
}

run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls',
                //'--add-modules', 'java.xml.bind',                
                //'--add-modules', 'java.se.ee',
                '--add-exports', 'javafx.graphics/com.sun.javafx.util=ALL-UNNAMED',
                '--add-exports', 'javafx.base/com.sun.javafx.reflect=ALL-UNNAMED', 
                '--add-exports', 'javafx.base/com.sun.javafx.beans=ALL-UNNAMED'
                //--add-exports=javafx.graphics/com.sun.glass.utils=ALL-UNNAMED 
                //--add-exports=javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED
        ]
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'    
    compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
    compile 'org.hibernate:hibernate-core:5.3.6.Final'
    compile group: 'postgresql', name: 'postgresql', version: '9.4.1208-jdbc42-atlassian-hosted'
    compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.1'
    compile group: 'org.jsoup', name: 'jsoup', version: '1.11.3'
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
    compile group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0.1'
    compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.1'
    compile group: 'javax.activation', name: 'activation', version: '1.1.1'

    compile "org.openjfx:javafx-base:11:$platform"
    compile "org.openjfx:javafx-graphics:11:$platform"
    compile "org.openjfx:javafx-controls:11:$platform"
    compile "org.openjfx:javafx-fxml:11:$platform"
    compile "org.openjfx:javafx-web:11:$platform"
}

如您所见,我添加了javax.xml.bind作为依赖项,但是休眠似乎仍然忽略了它。但是,如果我尝试添加jvm参数'--add-modules', 'java.xml.bind',则会得到java.lang.module.ResolutionException: Modules activation and java.activation export package javax.activation to module org.jsoup。那么,我该如何解决这个问题?

编辑:IDE(在我的情况下为eclipse)了解com.sun.xml.bind.v2.ContextFactory类(自动完成功能适用于该类)。

1 个答案:

答案 0 :(得分:0)

尝试添加此依赖项:

compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.1'

它应该可以工作。