Faced with a common problem - determining the path to xml.

Main.java:

Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml")); 

Project structure:

enter image description here

This question is similar to questions (in fact):

  1. JavaFx application does not start!
  2. Does not open .jar (IDEA IntelliJ)

However, they do not help solve my problem. How does this bootloader work?

Creating a JAVA FX project with MAVEN - http://devcolibri.com/3264

  • make a minimal example run and add it to the question. - Mikhail Vaysman
  • @MikhailVaysman, what else can I add to the question? Your solution is suitable when creating a Java FX project. I created a maven project. - bsuart
  • Add a pom and a minimum set of files to the question so that you can build a project and see the problem. - Mikhail Vaysman

1 answer 1

In the sample package

 public class Controller { } public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample/sample.fxml")); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 300, 275)); primaryStage.show(); } public static void main(String[] args) { launch(args); } } 

This file must be in resources / sample

 <?import javafx.scene.layout.GridPane?> <GridPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"> </GridPane> 

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>trash</groupId> <artifactId>javafx-1</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>sample.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 

After building the jar, you can run it

 java -jar target/javafx-1-1.0-SNAPSHOT-jar-with-dependencies.jar 

Laid out the full application code on Github JavaFX Maven Example

  • I know that it will run on any other project. I have a Maven project, the error still remains - bsuart
  • I added a pom file. and checked - it works for me. - Mikhail Vaysman