当前环境:macos jdk 1.8 IDEA
目录
方式1 直接idea创建
直接可运行
转mvn,方便集成管理其他依赖
转换后,添加依赖
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>sample.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.1</version>
</dependency>
</dependencies>
运行后报错,报错内容如下,大概是找不到配置文件
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at sample.Main.start(Main.java:14)
报错解决参考JavaFx出现错误Caused by: java.lang.NullPointerException: Location is required的解决方法 - Stars-one - 博客园
最终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>groupId</groupId>
<artifactId>fx1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!-- <release>8</release>-->
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>sample.Main</mainClass>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<!-- 这里是放在 src/main/java-->
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.fxml</include>
<include>**/fxml/*.fxml</include>
<!-- 如果想要弄个包名专门放fxml文件,像上一行这样添加设置 -->
<!-- 之后,使用getResource("fxml/xx.fxml")这样子 -->
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
方式2 使用mvn创建
参考:Getting Started with JavaFX 提到的方式
直接使用mvn创建, 如果你本地mvn已经配置好,则执行执行,中间会让你输入你的项目名称
mvn archetype:generate \
-DarchetypeGroupId=org.openjfx \
-DarchetypeArtifactId=javafx-archetype-simple \
-DarchetypeVersion=0.0.3 \
-DgroupId=org.openjfx \
-DartifactId=sample \
-Dversion=1.0.0 \
-Djavafx-version=17.0.1
创建后内容目录如下,注意这里默认是以jdk11环境运行,需要个人本地jdk环境的适配,如果你的版本低于11,需要修改项目的运行环境 ,可以参考解决“Error:java: 无效的源发行版: 11”问题_My_Sunshine_appear的博客-CSDN博客_无效的发行版本11
代码中类似下面的写法jdk8不支持,所以需要高版本jdk才可
可以参考:IDEA+maven+javafx(java 1.8)入坑记录
参考:
IDEA+maven+javafx(java 1.8)入坑记录JavaFx出现错误Caused by: java.lang.NullPointerException: Location is required的解决方法 - Stars-one - 博客园