如何为Java 11 + JavaFX Hibernate模块修复module-info.java

时间:2019-08-29 02:21:00

标签: java maven jpa javafx modularity

我正在使用Java11创建一个简单的测试应用程序,以学习JPA,Hibernate,一些Java模块化以及以后的功能(带有Dagger的CDI),并且当我使用module-info.java时遇到编译问题

我为一些DAO repositories做了一些simple classes,这些工作都被JPA注释以对其进行测试,并且一切顺利地在我Main class上成功完成,其中我成功插入和检索了一些对象。 注意:到目前为止,我还没有module-info.java

现在,我正在制作一些FXML(使用scenebuilder)。我之前已经做过,而且我必须创建一个module-info.java来解决一些JavaFX问题(我认为这与Java反射有关):我在其他project中做到了这一点,并且按预期工作。这是由Intellij警告解决的,这些警告是通过IntelliJ解决它们或某些运行时异常而解决的,通知我app.a软件包需要打开到app.b软件包,然后我手动进行操作。这是我第一次使用module-info.java

因此,当我尝试通过另一类MainFX使用gui时,会遇到一些错误(我将尝试解决,但不是这个问题的意思):

Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x71b9e075) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x71b9e075
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at app.control.MainControl.loadFXML(MainControl.java:22)
    at app.MainFX.start(MainFX.java:42)


正是在这一点上,我试图添加一个module-info.java并解决了IntelliJ捕获的所有导入依赖关系,并添加了一些我的模块依赖关系:


module bartest {
    requires com.google.common;
    requires javafx.controls;
    requires javafx.graphics;
    requires javafx.fxml;
    requires flogger;
    requires java.logging;
    requires java.persistence;


    opens app;

}


但是我尝试过的任何事情都会得到java.lang.module.ResolutionException

Information:java: compiler message file broken: key=compiler.misc.msg.bug arguments=11.0.4, {1}, {2}, {3}, {4}, {5}, {6}, {7}
Information:java: java.lang.module.ResolutionException: Module jakarta.activation contains package javax.activation, module java.activation exports package javax.activation to jakarta.activation
Information:java:   at java.base/java.lang.module.Resolver.resolveFail(Resolver.java:885)
Information:java:   at java.base/java.lang.module.Resolver.failTwoSuppliers(Resolver.java:789)
Information:java:   at java.base/java.lang.module.Resolver.checkExportSuppliers(Resolver.java:718)
Information:java:   at java.base/java.lang.module.Resolver.finish(Resolver.java:362)
Information:java:   at java.base/java.lang.module.Configuration.<init>(Configuration.java:130)
Information:java:   at java.base/java.lang.module.Configuration.resolveAndBind(Configuration.java:484)
Information:java:   at java.base/java.lang.module.Configuration.resolveAndBind(Configuration.java:288)
Information:java:   at jdk.compiler/com.sun.tools.javac.file.JavacFileManager.getServiceLoader(JavacFileManager.java:985)
Information:java:   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Information:java:   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Information:java:   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Information:java:   at java.base/java.lang.reflect.Method.invoke(Method.java:566)
Information:java:   at org.jetbrains.jps.javac.JavacMain$2.invoke(JavacMain.java:290)
Information:java:   at com.sun.proxy.$Proxy24.getServiceLoader(Unknown Source)
Information:java:   at com.sun.proxy.$Proxy25.getServiceLoader(Unknown Source)
Information:java:   at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.initProcessorLoader(JavacProcessingEnvironment.java:261)
Information:java:   at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.<init>(JavacProcessingEnvironment.java:237)
Information:java:   at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.instance(JavacProcessingEnvironment.java:193)
Information:java:   at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.initProcessAnnotations(JavaCompiler.java:1136)
Information:java:   at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:922)
Information:java:   at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
Information:java:   at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:147)
Information:java:   at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
Information:java:   at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
Information:java:   at org.jetbrains.jps.javac.JavacMain.compile(JavacMain.java:195)
Information:java:   at org.jetbrains.jps.incremental.java.JavaBuilder.compileJava(JavaBuilder.java:460)
Information:java:   at org.jetbrains.jps.incremental.java.JavaBuilder.compile(JavaBuilder.java:330)
Information:java:   at org.jetbrains.jps.incremental.java.JavaBuilder.doBuild(JavaBuilder.java:255)
Information:java:   at org.jetbrains.jps.incremental.java.JavaBuilder.build(JavaBuilder.java:213)
Information:java:   at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1324)
Information:java:   at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:1004)
Information:java:   at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:1071)
Information:java:   at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:965)
Information:java:   at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:794)
Information:java:   at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:376)
Information:java:   at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:178)
Information:java:   at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:139)
Information:java:   at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:288)
Information:java:   at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:121)
Information:java:   at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler.lambda$channelRead0$0(BuildMain.java:228)
Information:java:   at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
Information:java:   at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
Information:java:   at java.base/java.lang.Thread.run(Thread.java:834)
Information:java: Errors occurred while compiling module 'bartest'
Information:javac 11.0.4 was used to compile java sources
Information:29/08/2019 4:03 a. m. - Build completed with 1 error and 0 warnings in 4 s 405 ms
Error:java: Compilation failed: internal java compiler error

当我删除module-info.java时,我开始获得成功的构建,并且非javaFX代码也可以正常工作。我想如果没有module-info.java,module-info会自动正确生成。

所以我的module-info.java无法编译,没有编译,但是我不能运行JavaFX,而如果没有module-info.java文件,它可能可以解决,但是我想知道是什么原因造成的如您所知,我对大多数概念(Maven,JPA,模块化)还算陌生,我的测试目标是学习,所有帮助将不胜感激。

我认为可能会影响的其他原因是我缺乏使用maven pom.xml文件的经验,我基本上是让Intellij生成我在互联网上搜索的依赖项和插件标签,但可能是错误的

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <artifactId>bartest</artifactId>
  <groupId>com.kurama</groupId>
  <modelVersion>4.0.0</modelVersion>
  <version>1.0-SNAPSHOT</version>
  <name>J_HibernateDaggerTest</name>
  <url>http://maven.apache.org</url>
  <properties>
    <java.version>11.0.4</java.version>
    <hibernate.version>5.3.11.Final</hibernate.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <release>${java.version}</release>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
        <groupId>org.apache.maven.plugins</groupId>
        <version>3.8.1</version>
      </plugin>
      <plugin>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <configuration>
          <mainClass>app.MainFX</mainClass>
        </configuration>
        <version>0.0.3</version>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <dependencies>
          <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>6.2</version>
          </dependency>
        </dependencies>
        <configuration>
          <forkCount>0</forkCount>
        </configuration>
      </plugin>
    </plugins>
  </build>


  <dependencies>
    <dependency>
      <artifactId>javafx-controls</artifactId>
      <groupId>org.openjfx</groupId>
      <version>11.0.2</version>
    </dependency>
    <dependency>
      <artifactId>javafx-fxml</artifactId>
      <groupId>org.openjfx</groupId>
      <version>11.0.2</version>
    </dependency>
    <dependency>
      <artifactId>guava</artifactId>
      <groupId>com.google.guava</groupId>
      <version>LATEST</version>
    </dependency>
    <dependency>
      <artifactId>mariadb-java-client</artifactId>
      <groupId>org.mariadb.jdbc</groupId>
      <version>2.4.2</version>
    </dependency>
    <dependency>
      <artifactId>flogger</artifactId>
      <groupId>com.google.flogger</groupId>
      <version>0.4</version>
    </dependency>
    <dependency>
      <artifactId>flogger-system-backend</artifactId>
      <groupId>com.google.flogger</groupId>
      <version>0.4</version>
    </dependency>
    <dependency>
      <artifactId>controlsfx</artifactId>
      <groupId>org.controlsfx</groupId>
      <version>11.0.0</version>
    </dependency>
    <dependency>
      <artifactId>jfxtras-controls</artifactId>
      <groupId>org.jfxtras</groupId>
      <version>10.0-r1</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${hibernate.version}</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate.version}</version>
    </dependency>
    <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.2</version>
    </dependency>
  </dependencies>
</project>

编辑1:
HERE用户Gili指出:

  

“ Eclipse基金会有taken over JavaEE developmentjavax.activation已按wiki.eclipse.org/New_Maven_Coordinates移至jakarta.activation

所以我尝试将以下部门添加到pom中

    <dependency>
      <groupId>jakarta.activation</groupId>
      <artifactId>jakarta.activation-api</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>jakarta.persistence</groupId>
      <artifactId>jakarta.persistence-api</artifactId>
      <version>2.2.2</version>
    </dependency>
    <dependency>
      <groupId>jakarta.transaction</groupId>
      <artifactId>jakarta.transaction-api</artifactId>
      <version>1.3.1</version>
    </dependency>

在构建时仍然失败,并且我在module-info文件上收到警告

Error:java: java.lang.module.ResolutionException: Modules java.activation and jakarta.activation export package javax.activation to module error.prone.annotations
Information:java: java.lang.module.ResolutionException: Modules jakarta.activation and java.activation export package javax.activation to module javafx.webEmpty
Information:java: Errors occurred while compiling module 'bartest'
Information:javac 11.0.4 was used to compile java sources
Information:29/08/2019 4:03 a. m. - Build completed with 1 error and 0 warnings in 4 s 405 ms
Error:java: Compilation failed: internal java compiler error


编辑2: 我确实遵循过IntelliJ can't recognize JavaFX 11 with OpenJDK 11 之前导致我进入running JavaFX projects的情况,但是当我按照那里的说明添加module-info.java时,我得到了上面粘贴的异常。在建立。显然,模块jakarta.activationjava.activation都存在导出包javax.activation的问题,但是我不知道如何解决这个问题

1 个答案:

答案 0 :(得分:2)

此补丁程序已经引起了我的注意...出于文档原因在这里无法解释

我添加了https://stackoverflow.com/a/48136912/11970095之后

    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
      <version>2.3.2</version>
    </dependency>

消除此问题解决了一个大问题,唯一缺少的是module-info.java,它在修复了必需的模块后现在看起来像这样

module bartest {
    requires com.google.common;
    requires javafx.controls;
    requires javafx.graphics;
    requires javafx.fxml;
    requires flogger;
    requires java.logging;
    requires net.bytebuddy;
    requires java.sql;
    requires java.xml.bind;

    opens app.control to javafx.fxml;
    opens app.model to org.hibernate.orm.core;

    opens app;

}

现在一切正常。