有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在JDK11之后交付javafx应用程序需要javafxmavenplugin吗?

我试图找出创建hello world javafx应用程序所需的最小值pom.xml,该应用程序可以作为jar+./lib提供给客户端,并与java -jar一起运行

我用过:

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>com.example.javafx</groupId>
    <artifactId>javafx-jdk14-example</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.example.javafx.app.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>14</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>14</version>
            <classifier>linux</classifier>
        </dependency>        
    </dependencies>    
</project>

Main。java

package com.example.javafx.app;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
 
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);  // Override default
        grid.setHgap(10);
        grid.setVgap(12);        
        
        Button btn = new Button();

        
        btn.setText("Clickme");
        btn.setOnAction((t) -> System.out.println("Clicked!"));
        
        grid.add(btn, 0, 0);
        primaryStage.setScene(new Scene(grid, 640, 480));
        primaryStage.show();
    }
}

请注意,我不会在我的应用程序上使用module-info.java

但当我运行它时:

mvn clean package
cd target
/jdk14/bin/java -jar javafx-jdk14-example-1.0.0.jar

我得到:

Error: JavaFX runtime components are missing, and are required to run this application

如果我对JDK10使用相同的方法(也使用compiler.source/target=10并注释掉org.openjfxdeps),那么上面的方法可以很好地工作

有没有其他方法(在JDK11+上)构建应用程序,然后能够从目标目录中运行应用程序,而无需使用javafx-maven-plugin


共 (1) 个答案

  1. # 1 楼答案

    在@Slaw的帮助下,我找到了运行javafx应用程序(创建时没有javafx-maven-plugin)的方法:

    cd target
    java -p lib/  add-modules javafx.controls -jar javafx-jdk14-example-1.0.0.jar
    

    我需要显式地添加javafx.controls模块

    底线:

    javafx-maven-plugin不是构建和发布javafx应用程序所必需的