它仍然找不到FXML位置。我搞砸了 输出目录和 目录和 进入FXMLLoader的URL无效:
Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));
此应用程序在哪里寻找资源?
目录:
在:
PROJECT / src / main / java / mypackage / Main.java
PROJECT / src / main / resources / fxml / ui.fxml
出局:
PROJECT / target / AppName-1.0-SNAPSHOT.jar
PROJECT / target / AppName-1.0-SNAPSHOT-shaded.jar
PROJECT / target / classes / myPackage / Main.class
PROJECT / target / fxml / ui.fxml
我的POM:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release> <!--or <release>10</release>-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.potatospy.NewMain</mainClass>
</transformer>
</transformers>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
答案 0 :(得分:2)
对于初学者来说,第一个问题与您的资源有关:
父根= FXMLLoader.load(getClass()。getResource(“ fxml / ui.fxml”));
如果使用getResource
,则必须使用绝对路径:
Parent root = FXMLLoader.load(getClass().getResource("/fxml/ui.fxml"));
StackOverflow上对此问题有很多疑问,此answer是非常有效的参考。
maven-shade-plugin
假设您的com.potatospy.NewMain
类是不扩展Application
的{{3}},并且您的来源(在src/main/java/
下)是:
和您的资源(在src/main/resources
下)是
如果您想做一个阴影/脂肪罐,而又不修改默认的maven-resources-plugin,那么仅使用maven-compile-plugin
和{{1 }}来自pom的插件:
maven-shade-plugin
然后您可以运行您的应用程序:
mvn clean package
请注意,已应用默认资源插件,并且您的文件已添加到java -jar target/AppName-1.0-SNAPSHOT.jar
:
maven-resource-plugin
但是,如果像在pom中一样添加资源插件,则在运行它时,您会注意到文件已添加到:
打包完成后,甚至没有将fxml文件添加到jar中!
如果需要包括资源插件,则将需要以下内容:
target/classes
请注意,我已将<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
</plugin>
添加到输出目录(因此资源已包含在其中)。