使用javafxplugin和hibernate时无法在IDE外部运行JavaFX应用程序

时间:2019-01-04 18:25:43

标签: java gradle javafx javafx-11 openjfx

我使用gradle:installDist分发JavaFX应用程序时遇到问题。我将问题简化为一个基本示例。

我的应用程序具有以下设置:

  • OpenJDK 11
  • OpenJFX 11
  • 5.0版
  • 休眠5.4
  • h2database 1.4.197

build.gradle:

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'de.testing'
version '1.0-SNAPSHOT'



repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.0.Final'
    compile group: 'com.h2database', name: 'h2', version: '1.4.197'
}

mainClassName = 'de.testing.Main'

代码:

package de.testing;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("thePersistenceUnit");
        EntityManager em = emf.createEntityManager();
    }
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="thePersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:h2:~/testing"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

当我在IDE中运行它时,一切都很好。 但是,当我使用gradle:installDist分发应用程序并尝试运行开始脚本时,出现以下异常:

.\testing.bat
Jan. 04, 2019 6:53:22 NACHM. org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
        name: thePersistenceUnit
        ...]
Jan. 04, 2019 6:53:22 NACHM. org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {[WORKING]}
Jan. 04, 2019 6:53:22 NACHM. org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Exception in thread "main" java.lang.NoClassDefFoundError: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:345)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:337)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.cfg.Environment.<clinit>(Environment.java:230)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:78)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:67)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:202)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:174)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:171)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:119)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:61)
        at org.hibernate.orm.core@5.4.0.Final/org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:50)
        at java.persistence@2.2/javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
        at java.persistence@2.2/javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at testing@1.0-SNAPSHOT/de.testing.Main.main(Main.java:9)
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 15 more

但是,当我删除

id 'org.openjfx.javafxplugin' version '0.0.5'
从我的gradle插件列表中

并重新分发应用程序,使用gradle:installDist中分发的开始脚本,它可以正常运行。

1 个答案:

答案 0 :(得分:1)

可以使用您发布的代码轻松重现该问题。

我可以确认它与javafx-gradle-plugin有关,后者又使用java9-modularity插件。 here之前已经提过一个问题,可能与它有关。

基本上,问题在于使用此插件将所有依赖项添加到模块路径,而没有一个添加到类路径。

IDE为何起作用?

“从IDE运行”时,我假设您正在运行run任务:

./gradle run

那很好。如果您这样做:

./gradle --info run

它将打印命令行。像这样:

/Users/<user>/Downloads/jdk-11.0.1.jdk/Contents/Home/bin/java \
     --add-modules javafx.controls,javafx.fxml \
     --module-path  /path/to/your-project/build/classes/java/main: \
         /path/to/your-project/build/resources/main: \
         /Users/<user>/.gradle/caches/modules-2/files-2.1/org.hibernate/hibernate-core/5.4.0.Final/70...2b/hibernate-core-5.4.0.Final.jar: \
         ...
         /Users/<user>/.gradle/caches/modules-2/files-2.1/com.sun.xml.fastinfoset/FastInfoset/1.2.15/bb..e8/FastInfoset-1.2.15.jar \
     -Dfile.encoding=UTF-8 -Duser.variant \
     -cp /path/to/your-project/build/classes/java/main: \
         /path/to/your-project/build/resources/main: \
         /Users/<user>/.gradle/caches/modules-2/files-2.1/org.hibernate/hibernate-core/5.4.0.Final/70...2b/hibernate-core-5.4.0.Final.jar: \
         ...
         /Users/<user>/.gradle/caches/modules-2/files-2.1/com.sun.xml.fastinfoset/FastInfoset/1.2.15/bb..e8/FastInfoset-1.2.15.jar \
     de.testing.Main

build/install/your-project/lib运行

因此,您实际上可以尝试从installDist任务获得的lib文件夹中的所有jar中手动运行。在终端上:

./gradlew installDist
cd build/install/your-project/lib
/Users/<user>/Downloads/jdk-11.0.1.jdk/Contents/Home/bin/java \
     --add-modules javafx.controls,javafx.fxml \
     --module-path . \
     -cp '*' \
     de.testing.Main

应该可以。

问题

那为什么运行脚本失败?

如果您编辑脚本:

cd build/install/your-project/bin
nano your-project

您可以验证以下行:

...
esac

CLASSPATH=

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
...

包含一个空的类路径。如果您看到了最后的选项:

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $YOUR-PROJECT_OPTS -classpath "\"$CLASSPATH\"" de.testing.Main "$APP_ARGS"

这意味着最终的命令行将类似于:

/Users/<user>/Downloads/jdk-11.0.1.jdk/Contents/Home/bin/java \
     --add-modules javafx.controls,javafx.fxml \
     --module-path . \
     -classpath \        // <--- Nothing is added here!
     de.testing.Main

这实际上开始,因为所有的jar已添加到模块路径中。但是由于类路径为空而失败,并发布了异常。

可能的修复

手动修复

一种可能的解决方法是在运行installDist任务并添加类路径后编辑脚本,例如:

CLASSPATH="../lib/*"

保存脚本,然后运行:

cd build/install/your-project/bin
./your-project

应该可以。

修改installDist任务

很显然,您不会手动执行此操作,因此这是对startScripts任务的修复,可以将其添加到build.gradle文件中:

tasks.startScripts {         doLast {             def scriptFile =文件“ $ {outputDir} / $ {applicationName}”             scriptFile.text = scriptFile.text.replace('CLASSPATH =','CLASSPATH = \'$ APP_HOME / lib / * \'')         }     }

删除插件

其他可能的解决方案是删除插件并使用旧方法:

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.0.Final'
    compile group: 'com.h2database', name: 'h2', version: '1.4.197'

    compile "org.openjfx:javafx-base:11.0.1:mac"
    compile "org.openjfx:javafx-controls:11.0.1:mac"
    compile "org.openjfx:javafx-graphics:11.0.1:mac"
    compile "org.openjfx:javafx-fxml:11.0.1:mac"
}

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

run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls,javafx.fxml'
        ]
    }
}

运行installDist任务后,您可以验证脚本是否包含完整的类路径:

CLASSPATH=$APP_HOME/lib/your-project.jar:$APP_HOME/lib/hibernate-core-5.4.0.Final.jar:...:$APP_HOME/lib/FastInfoset-1.2.15.jar

阴影罐

另一种解决方案,保留插件,但创建一个影子罐:

jar {
    manifest {
        attributes 'Main-Class': 'de.testing.Launcher'
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

其中Launcher是不扩展Application的类:

public class Launcher {

    public static void main(String[] args) {
        Main.main(args);
    }
}

现在,您可以运行jar任务,它将创建一个包含所有依赖项(包括JavaFX依赖项)的影子jar。然后您可以运行:

./gradlew jar
cd build/libs
java -jar your-project.jar

提出问题

最后,我建议您提出一个问题here,并关联此问题。