好吧,即時通訊對於javafx和一般編程來說相當新穎。我從來沒有真正打包過一個程序,因爲我從來沒有需要在IDE之外運行它們。我需要打包我的javafx程序,並且必須使用maven來處理所有其他依賴關係。使用Javafx Maven插件編譯項目後,程序無法找到資源
在包含pom文件的目錄中使用命令mvn jfx:jar編譯程序後,構建成功並生成我的jar。當通過WINRar檢查時,它包含以前在根目錄的my/src/main/resource文件夾中的所有文件。
但是,當使用java -jar(nameofjar).jar運行時,它不會啓動,而是會產生幾個錯誤,這些錯誤都與無法找到特定資源有關,特別是FXML以及其他資源。
Refrences到文件
welcome.fxml
public void start(Stage primaryStage) {
try {
AnchorPane pane = FXMLLoader.load(MainApp.class.getResource("welcome.fxml"));
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle("HeelBot Beta "+versionNumber);
primaryStage.getIcons().add(new Image("Icon.png"));
primaryStage.setResizable(true);
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
defaultProperties.properties
public class PropertiesManager {
public static void editProperty(String key, String value) throws IOException {
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("src/main/resources/defaultProperties.properties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties
// from last invocation
in = new FileInputStream("src/main/resources/appProperties.properties");
applicationProps.load(in);
in.close();
//set properties
applicationProps.setProperty(key,value);
FileOutputStream out = new FileOutputStream("src/main/resources/appProperties.properties");
applicationProps.store(out, "---No Comment---");
out.close();
}
POM文件
<?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.coursecruncher</groupId>
<artifactId>heelbot</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.4</version>
<configuration>
<mainClass>MainApp</mainClass>
<verbose>true</verbose>
<jfxMainAppJarName>heelbot.jar</jfxMainAppJarName>
<deployDir>${project.basedir}/src/main/resources</deployDir>
<updateExistingJar>true</updateExistingJar>
<allPermissions>true</allPermissions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
</project>
項目結構
├── configure
├── configure.in
├── src
│ ├── main
│ │ ├── java
│ │ │ ├── CoreController(class)
│ │ │ ├── Login(class)
│ │ │ ├── LoginController(class)
│ │ │ ├── MainApp(class)
│ │ │ └── PropertiesManager(class)
│ │ │
│ │ ├── resources
│ │ │ ├── appProperties.properties
│ │ │ ├── defaultProperties.properties
│ │ │ ├── welcome.fxml
│ │ │ ├── core.fxml
│ │ │ └── Heelbot.css
錯誤
enter code here
java.io.FileNotFoundException: src\main\resources\defaultProperties.properties (
The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at PropertiesManager.editProperty(PropertiesManager.java:11)
at LoginController.initialize(LoginController.java:60)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
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 Main.start(Main.java:53)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Platfor
mImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.
java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformI
mpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
at java.lang.Thread.run(Thread.java:745)
java.io.FileNotFoundException: src\main\resources\defaultProperties.properties (
The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at PropertiesManager.queryProperty(PropertiesManager.java:34)
at LoginController.initialize(LoginController.java:106)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
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 Main.start(Main.java:53)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Platfor
mImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.
java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformI
mpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
at java.lang.Thread.run(Thread.java:745)
Nov 12, 2015 5:12:08 PM Main start
SEVERE: null
javafx.fxml.LoadException:
file:/D:/Programing%20Data/Projects/CourseCruncher/target/jfx/app/heelbot.jar!/w
elcome.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
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 Main.start(Main.java:53)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Platfor
mImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.
java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformI
mpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at PropertiesManager.queryProperty(PropertiesManager.java:39)
at LoginController.initialize(LoginController.java:106)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
我懷疑我的POM配置可能是什麼原因造成的問題。我不太確定如何正確格式化,並嘗試各種組合和教程都無濟於事。我所得到的最接近使用該POM
備用POM文件
<?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.courecruncher</groupId>
<artifactId>heelbot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HeelBotBeta1</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeScope>system</excludeScope>
<excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>package-jar</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${env.JAVA_HOME}/bin/javafxpackager</executable>
<arguments>
<argument>-createjar</argument>
<argument>-nocss2bin</argument>
<argument>-appclass</argument>
<argument>MainApp</argument>
<argument>-srcdir</argument>
<argument>${project.build.directory}/classes</argument>
<argument>-outdir</argument>
<argument>.</argument>
<argument>-outdir</argument>
<argument>${project.build.directory}</argument>
<argument>-outfile</argument>
<argument>${project.artifactId}-app</argument>
<argument>-v</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
這將導致它找到FXML文件,但仍然拋出一個錯誤的屬性文件,我將不得不手動添加屬性文件放到項目目錄中。我如何讓打包的jar能夠找到自己的資源,而不是尋找jar之外的資源?
您的文章的大小反映了有更多的人會有更多的時間來幫助你。您最好嘗試創建一個[MCVE](http://stackoverflow.com/help/mcve)。 –