在IntelliJ IDEA中使用Maven构建JavaFX应用程序。

huangapple go评论65阅读模式
英文:

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&#39;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&#39;t run. I&#39;ve checked included manifest and there wasn&#39;t entry point. I&#39;ve added it, but the file still doesn&#39;t run. That&#39;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

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    
    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    
      &lt;groupId&gt;org.example&lt;/groupId&gt;
      &lt;artifactId&gt;Refinement&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    
    
      &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;maven.compiler.source&gt;1.7&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.7&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
    
      &lt;dependencies&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
          &lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
          &lt;version&gt;11.0.2&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.openjfx/javafx-maven-plugin --&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
          &lt;artifactId&gt;javafx-maven-plugin&lt;/artifactId&gt;
          &lt;version&gt;0.0.4&lt;/version&gt;
        &lt;/dependency&gt;
    
      &lt;/dependencies&gt;
    
      &lt;build&gt;
        &lt;plugins&gt;
          &lt;plugin&gt;
            &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
            &lt;artifactId&gt;javafx-maven-plugin&lt;/artifactId&gt;
            &lt;version&gt;0.0.4&lt;/version&gt;
            &lt;configuration&gt;
              &lt;mainClass&gt;org.example.App&lt;/mainClass&gt;
            &lt;/configuration&gt;
          &lt;/plugin&gt;
        &lt;/plugins&gt;
      &lt;/build&gt;
    
    
    &lt;/project&gt;

And build log

    &quot;C:\Program Files\Java\jdk-15\bin\java.exe&quot; -Dmaven.multiModuleProjectDirectory=C:\Users\Комп\Documents\javaProjects\Refinement &quot;-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3&quot; &quot;-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven3\bin\m2.conf&quot; &quot;-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\plugins\maven\lib\maven-event-listener.jar&quot; &quot;-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&quot; -Dfile.encoding=UTF-8 -classpath &quot;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&quot; org.codehaus.classworlds.Launcher -Didea.version=2020.2.2 package
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------------&lt; org.example:Refinement &gt;-----------------------
    [INFO] Building Refinement 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Refinement ---
    [INFO] Using &#39;UTF-8&#39; 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 &#39;UTF-8&#39; 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&#39;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&#39;m not sure if user&#39;s machine will have JavaFX installed, and I don&#39;t want to rely on this.

Also I&#39;ve tried to rebuild the pom file as shown in &lt;a href=&quot;https://webtechie.be/post/2019-04-01-pijava-part-4-building-a-minimal-javafx-11-application-with-maven/&quot;&gt;Building a minimal JavaFX 11 application with Maven&lt;/a&gt; article. This new version also throws an error:

    Exception in thread &quot;main&quot; 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&#39;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 &quot;Gluon Features&quot;, &quot;CloudLink&quot; and &quot;Attach&quot;.

[![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>



huangapple
  • 本文由 发表于 2020年10月6日 17:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64223514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定