首页 > 解决方案 > JavaFX 和 Maven - 构建和导出 JAR

问题描述

我用 JavaFX 建立了一个小项目。

主要阶段包含一个带有 WebView 的阶段。

我想导出这个项目,但是当我尝试构建 jar 工件时,错误是:

Error: Could not find or load main class Principale.WebViewMainClass
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

main(也是唯一的类)的代码是:

package Principale;

import javafx.application.*;
import javafx.collections.ListChangeListener;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Set;


public class WebViewMainClass extends Application {

        public static void main(String[] args) {
            Application.launch(args);

        }

        public void start(Stage primaryStage) {

            primaryStage.initStyle(StageStyle.UNDECORATED);

            primaryStage.setMaximized(true);

            WebView webView = new WebView();
            WebEngine webEngine = webView.getEngine();

            webView.setContextMenuEnabled(false);

            webView.getEngine().load("http://google.it/");
            webView.setZoom(0.5);

            VBox vBox = new VBox(webView);
            vBox.setFillWidth(true);

            Scene scene = new Scene(vBox, 1000,1000);


            primaryStage.setScene(scene);
            primaryStage.show();

            Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
            double h = screenBounds.getHeight();
            double w= screenBounds.getWidth();

            webView.setMinWidth(w);
            webView.setMinHeight(h);

            webView.getChildrenUnmodifiable().addListener(new ListChangeListener<Node>() {
                @Override public void onChanged(Change<? extends Node> change) {
                    Set<Node> deadSeaScrolls = webView.lookupAll(".scroll-bar");
                    for (Node scroll : deadSeaScrolls) {
                        scroll.setVisible(false);
                    }
                }
            });



        }
    }

清单是:

Manifest-Version: 1.0
Main-Class: Principale.WebViewMainClass

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>org.testWebView</groupId>
    <artifactId>webview</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>17-ea+11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>15</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>15</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-swing</artifactId>
            <version>17-ea+11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>17-ea+11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>17-ea+11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>17-ea+11</version>
        </dependency>


    </dependencies>
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>10</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>webview.WebViewMainClass</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            Principale.WebViewMainClass
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>Application.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </build>

</project>

我正在使用 Intellij IDE,这是我第一次使用 JavaFX 和 Maven。

标签: mavenjavafxbuildjarartifact

解决方案


推荐阅读