Spring Boot 2 / Kotlin / JDK9 - 引起:java.lang.ClassNotFoundException:javax.transaction.SystemException

时间:2018-03-23 16:30:02

标签: hibernate maven spring-boot kotlin java-9

实际上我正在使用Kotlin开发Spring Boot v2应用程序。我在版本9中使用JDK。

添加依赖项spring-boot-starter-data-jpa后,我收到了ClassNotFound错误:

  

引起:org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]中定义名称为'entityManagerFactory'的bean创建错误:init的调用方法失败;嵌套异常是java.lang.NoClassDefFoundError:javax / transaction / SystemException

我找到了一些针对该问题的文章,并在/ src / main / kotlin下添加module-info.java以及以下内容:

    module de.sample.backend {

//    requires java.xml.bind;
    requires javax.transaction.api;
//    requires validation.api;
    requires hibernate.jpa;
//    requires hibernate.validator;
    requires spring.beans;
    requires spring.core;
    requires spring.context;
    requires spring.tx;
    requires spring.web;
    requires spring.webmvc;
    requires spring.data.commons;
    requires spring.data.jpa;
    requires spring.boot;
    requires spring.boot.autoconfigure;
//    requires cache.api;

}

然后我会得到Caused by: java.lang.ArrayIndexOutOfBoundsException: 32800

  

[错误]无法执行目标   org.apache.maven.plugins:Maven的编译器插件:3.7.0:编译   项目naehmeisterei-services上的(default-compile):执行   default-compile of goal   org.apache.maven.plugins:maven-compiler-plugin:3.7.0:编译失败:   32800 - > [帮助1]   org.apache.maven.lifecycle.LifecycleExecutionException:失败   执行目标   org.apache.maven.plugins:Maven的编译器插件:3.7.0:编译   项目naehmeisterei-services上的(default-compile):执行   default-compile of goal   org.apache.maven.plugins:maven-compiler-plugin:3.7.0:编译失败:   32800       在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)       在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)       在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)       在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject   (LifecycleModuleBuilder.java:117)       在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject   (LifecycleModuleBuilder.java:81)       在org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build   (SingleThreadedBuilder.java:51)       在org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)       在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309)       在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194)       在org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107)       在org.apache.maven.cli.MavenCli.execute(MavenCli.java:955)       在org.apache.maven.cli.MavenCli.doMain(MavenCli.java:290)       在org.apache.maven.cli.MavenCli.main(MavenCli.java:194)       at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:564)       在org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced   (Launcher.java:289)       在org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)       在org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode   (Launcher.java:415)       在org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)引起:   org.apache.maven.plugin.PluginExecutionException:执行   default-compile of goal   org.apache.maven.plugins:maven-compiler-plugin:3.7.0:编译失败:   32800

这是我完整的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>de.sample.backend</groupId>
    <artifactId>sample-services</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sample-services</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.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>-->
        <maven.compiler.source>1.9</maven.compiler.source>
        <maven.compiler.target>1.9</maven.compiler.target>
        <maven.compiler.release>9</maven.compiler.release>
        <kotlin.version>1.2.20</kotlin.version>
    </properties>

    <dependencies>

        <!-- Camunda - Spring Boot -->

        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
            <version>2.3.0</version>
        </dependency>


        <!-- Spring Boot -->

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

        <!-- https://jaxenter.de/spring-boot-2-actuator-2-0-68749 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Kotlin -->

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>

        <!-- JUnit -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Persistence / Database -->

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <!-- Firebase -->

        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>5.9.0</version>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Implementation-Version>${project.version}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                    <jdkToolchain>
                        <version>9</version>
                    </jdkToolchain>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

我实际上不知道如何解决这个问题。在另一台装有JDK 8的计算机上,它编译得很好。

有人可以帮忙吗?

谢谢!

更新

我使用spring initialzr创建了一个新的spring-boot项目: https://github.com/Chris81T/spring-boot-jdk9-kotlin-demo

如果没有任何更改,我会得到同样的错误。也许有人可以通过寻找它来实现它。那会很好: - )

1 个答案:

答案 0 :(得分:5)

您需要包含从Java 9弃用的所有javaee依赖项(替代方法是将--add-modules java.se.ee--add-modules ALL-SYSTEM添加到maven编译器选项以及启动应用程序的java但不建议这样做,因为类将在Java 11中消失。

1.您需要删除module-info.java文件并添加以下依赖项。

  1. 在maven中添加属性(因为surefire / failsafe 2.20.1有bug阻止在Java 9中正确运行测试):
  2. <properties>                                                                
        <argLine>--add-modules java.base</argLine>
     </properties>
    

    或者您可以切换到surefire 2.21.0 - 请参阅http://maven.apache.org/surefire/maven-surefire-plugin/java9.html

    我认为完整列表,您可以选择或只使用所有这些:

        <dependency>                                                                                                                                                         
            <groupId>org.glassfish.jaxb</groupId>                               
            <artifactId>jaxb-runtime</artifactId>                               
            <version>2.3.0</version>                                            
        </dependency>                                                           
        <dependency>                                                            
            <groupId>org.glassfish.jaxb</groupId>                               
            <artifactId>jaxb-core</artifactId>                                  
            <version>2.3.0</version>                                            
        </dependency>                                                           
        <dependency>                                                            
            <groupId>javax.xml.bind</groupId>                                   
            <artifactId>jaxb-api</artifactId>                                   
            <version>2.3.0</version>                                            
        </dependency>                                                           
        <dependency>                                                            
            <groupId>javax.activation</groupId>                                 
            <artifactId>javax.activation-api</artifactId>                       
            <version>1.2.0</version>                                            
        </dependency>                                                           
        <dependency>                                                            
            <groupId>javax.annotation</groupId>                                 
            <artifactId>javax.annotation-api</artifactId>                       
            <version>1.3.1</version>                                            
        </dependency>                                                           
        <dependency>                                                            
            <groupId>javax.transaction</groupId>                                
            <artifactId>javax.transaction-api</artifactId>                      
            <version>1.2</version>                                              
        </dependency>                                                           
        <dependency>                                                            
            <groupId>javax.xml.ws</groupId>                                     
            <artifactId>jaxws-api</artifactId>                                  
            <version>2.3.0</version>                                            
        </dependency>                                                           
        <dependency>                                                            
            <groupId>com.sun.xml.ws</groupId>                                   
            <artifactId>jaxws-rt</artifactId>                                   
            <version>2.3.0</version>                                            
        </dependency>