3

im trying to fix this for hours, I need help:

Its the not unknown problem, that javax is not part of java 9, I cant figure out how to get it run without the following exeption:

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in FILE C:\Users\Miles\Workspace\Projekte\SpringProjekte\RestForFun\resources\hibernate.cfg.xml. Message: null
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlFile(ConfigLoader.java:76)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:167)
at org.hibernate.cfg.Configuration.configure(Configuration.java:300)
at noobz.teamz.RestForFun.RestForFunApplication.main(RestForFunApplication.java:31)
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.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
... 5 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.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:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
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

this is my example:

import org.hibernate.cfg.Configuration;
import java.io.*;


public class RestForFunApplication {


    public static void main(String[] args) {


        RestForFunApplication rsf = new RestForFunApplication();

        File file = new File("resources\\hibernate.cfg.xml");
        if (!file.exists())
            System.out.println("no hibernate.cfg.xml");
        Configuration sessionFactory = new Configuration();
        sessionFactory.configure(file);
    }
}

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>noobz.teamz</groupId>
<artifactId>RestForFun</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>RestForFun</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.1.4</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.12.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search</artifactId>
        <version>5.8.2.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.2</version>
            <configuration>
                <release>9</release>
                <compilerArgs>
                    <arg>--add-modules</arg>
                    <arg>javax.xml.bind</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

this is my hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">db</property>
    <property name="hibernate.connection.username">usr</property>
    <property name="hibernate.connection.password">psswrd</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
    <property name="connection.pool_size">1</property>
</session-factory>

I added

--add-modules=java.xml.bind,java.activation

and

--add-modules java.se.ee

to intellij idea compiler-settings and also tried to run via console with this.

Nothing works, I still get the exception. Im allmost crying, is extremly nessecary to get this work.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Abgehter
  • 151
  • 1
  • 9
  • I recommend first trying to get it to work with Maven from the command line. Your pom says _javax.xml.bind_, but the module is called _java.xml.bind_ (no _x_ after _java_). Although that should have let to a different error... – Nicolai Parlog Jan 06 '18 at 17:48
  • The exception does look like _java.xml.bind_ is loaded, though. Otherwise the `JAXBException` would not be available - maybe [this Q&A can help](https://stackoverflow.com/q/36041656/2525313)? – Nicolai Parlog Jan 06 '18 at 17:51
  • 2
    Can you replace the dependency on `org.glassfish.jaxb:jaxb-core:2.3.0` with a dependency on `com.sun.xml.bind:jaxb-core:2.3.0`, `com.sun.xml.bind:jaxb-impl:2.3.0` and `javax.activation:javax.activation-api:1.2.0`? I think these are the right coordinates for the JAXB implementation (you have the correct coordinates for the JAXB API). Also you can drop the `--add-modules` from your POM as you are using the standalone version. – Alan Bateman Jan 07 '18 at 09:41

2 Answers2

12

That's a good question. But it can be simplified to the very short example, so you don't need to bother readers with spring or hibernate dependencies. Already this short code:

public static void main(String[] args) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);
}

will cause the problem (consider Foo be any POJO).

In fact the all existent solutions on S.O. which tell about this problem do not work properly (for instance, this "canonical" question: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9). I don't know wether it is a consequences of some updates on recent Java JDK (9.0.1), or I am cooking it wrongly. However. I personally think that playing with --add-module properties is not a clean way. I believe, that proper way would be use only maven dependencies. And I have found this needed minimal list, it is:

<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

So you don't need to explicitly specify --add-module anymore, just remove it.


P.S. In your particular code please also fix this property:

<java.version>1.8</java.version>

as you use Java-9.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • Your right, this changes made a huge effort! Thank you. Now if i changed back my code and started it there is a long debug list ending with java.lang.NoClassDefFoundError: javax/validation/ClockProvider. Im unable to find the right dependency (shame on me). Do you have a hint? I want have a success before marking as correct. Ty :) – Abgehter Jan 08 '18 at 10:37
  • Have you tried this dependency? https://mvnrepository.com/artifact/javax.validation/validation-api/2.0.1.Final – Andremoniy Jan 08 '18 at 13:51
  • But any way, this issue is out of scope of your initial question. If I have helped you to solve this particular issue, then accepting the answer would be fair – Andremoniy Jan 08 '18 at 13:51
  • This dependeny solved it! Please add it to your answer for the next. Thank you! Not only for the answer but also for this clue to find correct dependencies! – Abgehter Jan 08 '18 at 19:40
  • Thanks, after trying multiple other suggestions in other places this worked great for me. I'd upvote twice if I could! – Ryan P. May 15 '18 at 15:35
0

I've found (and checked) next set of dependencies:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.1</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>javax.activation-api</artifactId>
    <version>1.2.0</version>
</dependency>

See Upgrade to Java 10 now

UPDATE: for Java 11 it's enough:

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.4.0-b180830.0438</version>
    </dependency>

See Migrating to Java 11

Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197