SCAU-javaFX使用maven打包踩坑

打包步骤:

1.要在maven当中添加专门的依赖以及配置好版本信息

注意添加主类以及更改java版本信息

<?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>groupId</groupId>
    <artifactId>GC_ImageManager</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>18.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>18.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.8</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <!-- 打包项目及其依赖项到一个可执行的JAR文件中 -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>code.AppLaunch</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- 打包源代码到一个JAR文件中 -->
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>code.AppLaunch</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2.在projectStructrue里面选择modules创建

Maven,配置SourileFile还有ResourcesFile

3.先点clear,再点package

遇到的问题:

1.java.lang.UnsupportedClassVersionError

原因:使用了较高版本的JDK编译了代码,却在低版本的环境下运行

解决:

如何修复java.lang.UnsupportedClassVersionError | 码农家园

2.缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序

添加一个启动类,用这个启动类来启动主类,记得在maven里面改启动类

package code;

import javafx.application.Application;

public class Main {
    public static void main(String[] args) {
        Application.launch(AppLaunch.class,args);
//        AppLaunch.main(args);
    }
}

3.加载jar包当中的资源错误

统一写成:

getClass().getResource("/Default.css").toExternalForm()

会从当前jar包目录下取资源,不要使用资源特征符

原因:

如果使用绝对路径,jar包有自己无法被识别的特征符,无法找到文件

4.jar包加载外部的文件错误

把路径换成以下写法:

String s=file.getAbsolutePath();
System.out.println("file:"+s);

5.Caused by: java.lang.module.InvalidModuleDescriptorException: Unsupported major.minor version 62.0  

        stanford parser和jdk版本对应关系

J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

         62就是java18

        这个问题就是java编译的版本比运行环境的版本高导致的,我在这里使用的是maven,可以通过修改pom文件的属性完成:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.10.1</version>
    <configuration>
        <source>17</source>主要是注意这里,把版本改成比你的运行环境小的即可
        <target>17</target>
    </configuration>
</plugin>
<properties>
    <maven.compiler.source>16</maven.compiler.source>
    <maven.compiler.target>16</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <javafx.version>17</javafx.version>
</properties>

IDEA当中使用Maven

1.newproject的时候创建一个Maven项目

2.在已有项目当中打开projectstructure,添加module,配置sourceFile即可,如果要导入外部源代码,直接复制粘贴即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值