英文:
Building JavaFX app in IntelliJ IDEA using maven
问题
遇到了一些困难,试图使用Maven构建JavaFX应用程序。该应用程序在IDE中运行得很好。我运行了Maven的打包任务,它创建了一个JAR文件。问题是这个文件无法运行。我检查了包含的清单,发现没有入口点。我已经添加了它,但文件仍然无法运行。现在清单的样子如下(最后一行是手动添加的):
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: Комп
Build-Jdk: 15
Main-Class: org.example.App
POM文件如下:
<?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>org.example</groupId>
<artifactId>Refinement</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-maven-plugin -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>org.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
构建日志如下:
"C:\Program Files\Java\jdk-15\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\Users\Комп\Documents\javaProjects\Refinement "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\lib\idea_rt.jar=65494:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2020.2.2 package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< org.example:Refinement >-----------------------
[INFO] Building Refinement 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Refinement ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Комп\Documents\javaProjects\Refinement\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Refinement ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Refinement ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Комп\Documents\javaProjects\Refinement\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Refinement ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Refinement ---
[INFO] Surefire report directory: C:\Users\Комп\Documents\javaProjects\Refinement\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.example.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.052 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Refinement ---
[INFO] Building jar: C:\Users\Комп\Documents\javaProjects\Refinement\target\Refinement-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.456 s
[INFO] Finished at: 2020-10-06T12:04:09+03:00
[INFO] ------------------------------------------------------------------------
更新后:
我尝试从命令行界面启动JAR文件,却收到错误消息:
Error:
<details>
<summary>英文:</summary>
I've encountered some difficulties trying to build JavaFX app using maven. The app work just fine from IDE. I run maven package task and it creates JAR file. The problem is this file doesn't run. I've checked included manifest and there wasn't entry point. I've added it, but the file still doesn't run. That's how the manifest looks now (the last string was added manually):
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: Комп
Build-Jdk: 15
Main-Class: org.example.App
The pom file
<?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>org.example</groupId>
<artifactId>Refinement</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-maven-plugin -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>org.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
And build log
"C:\Program Files\Java\jdk-15\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\Users\Комп\Documents\javaProjects\Refinement "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\lib\idea_rt.jar=65494:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2020.2.2 package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< org.example:Refinement >-----------------------
[INFO] Building Refinement 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Refinement ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Комп\Documents\javaProjects\Refinement\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Refinement ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Refinement ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Комп\Documents\javaProjects\Refinement\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Refinement ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Refinement ---
[INFO] Surefire report directory: C:\Users\Комп\Documents\javaProjects\Refinement\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.example.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.052 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Refinement ---
[INFO] Building jar: C:\Users\Комп\Documents\javaProjects\Refinement\target\Refinement-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.456 s
[INFO] Finished at: 2020-10-06T12:04:09+03:00
[INFO] ------------------------------------------------------------------------
Updated
I've tried to launch JAR file from CLI and get the error:
Error: Could not find or load main class org.example.App
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Maybe I have to include JavaFX classes in the JAR file? I'm not sure if user's machine will have JavaFX installed, and I don't want to rely on this.
Also I've tried to rebuild the pom file as shown in <a href="https://webtechie.be/post/2019-04-01-pijava-part-4-building-a-minimal-javafx-11-application-with-maven/">Building a minimal JavaFX 11 application with Maven</a> article. This new version also throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at org.example.Launcher.main(Launcher.java:5)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more
Looks more verbose, but the essence is the same on my mind.
Checked PATH variable, it points to JavaFX lib folder.
</details>
# 答案1
**得分**: 2
要开始一个新的JavaFX项目,访问这个(全新的)网站是个好主意:https://start.gluon.io/。你会得到一个包含所有文件和README的ZIP文件,以便让你立即开始。
对于一个基本的应用程序,取消选择所有的“Gluon功能”,“CloudLink”和“Attach”。
[![start.gluon.io网页界面中的选择][1]][1]
[![start.gluon.io预览][2]][2]
[![start.gluon.io下载的ZIP文件][3]][3]
[1]: https://i.stack.imgur.com/Sy9o5.png
[2]: https://i.stack.imgur.com/Fbjgy.png
[3]: https://i.stack.imgur.com/Uns4a.png
<details>
<summary>英文:</summary>
To start a new JavaFX project, it's a good idea to visit this (brand new) website: https://start.gluon.io/. You will get a ZIP containing all the files and a README to get you started immediately.
For a basic app, deselect all "Gluon Features", "CloudLink" and "Attach".
[![start.gluon.io selections in web interface][1]][1]
[![start.gluon.io preview][2]][2]
[![start.gluon.io downloaded ZIP][3]][3]
[1]: https://i.stack.imgur.com/Sy9o5.png
[2]: https://i.stack.imgur.com/Fbjgy.png
[3]: https://i.stack.imgur.com/Uns4a.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论