【发布时间】:2019-09-02 12:52:05
【问题描述】:
当我运行我的应用程序时,出现“应用程序启动方法中的异常 [警告] java.lang.reflect.InvocationTargetException”。我尝试使用 fxml 路径但仍然无法正常工作。使用 try/catch 时出现“位置未设置错误”。
我也有 “无法在项目 Test-generator-Maven 上执行目标 org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli):执行 Java 类时发生异常。null:InvocationTargetException:异常应用启动方式:位置为必填项。” 但我不知道我的 pom.xml 出了什么问题
代码:
主要
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("../views/MainWindow.fxml"));
Scene scene = new Scene(root, 900, 400);
stage.setTitle("Generator testów");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args){
launch(args);
}
}
MainWindow.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane prefHeight="414.0" prefWidth="864.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mateuszm.controllers.MainWindowController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="252.0" minWidth="10.0" prefWidth="252.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="610.4000122070313" minWidth="10.0" prefWidth="503.20000000000005" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
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>com.mateuszm</groupId>
<artifactId>Test-generator-Maven</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
<classifier>mac</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<classifier>mac</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.mateuszm.main.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
-
在getResource中添加fxml的完整路径。 .getResource("/main/java/com/mateuszm/views/MainWindow.fxml"));
-
您的 FXML 文件位于
src/main/java下,但它必须位于src/main/resources下。 Maven 不处理src/main/java下的资源。 -
还是一样。我用 pom.xml 代码更新我的问题
-
getResource不允许“向上导航”:如果资源不在同一个包或子包中,则需要传递从类路径根开始的完整路径:getResource("/com/mateuszm/views/MainWindow.fxml") -
还是一样,可能是 pom 有问题?我不知道