JavaFX application error: no resources specified

I am new to javaFX and I am trying to run a simple application. its user interface is created using javaFX scenebuilder, and the main class should display the user interface, but nothing.

public class Main extends Application { public static void main(String[] args) { launch(Main.class, (String[])null); } @Override public void start(Stage primaryStage) {; try { AnchorPane root=(AnchorPane)FXMLLoader.load(Main.class.getResource("Main.fxml")); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.setTitle("Issue Tracking Lite Sample"); primaryStage.show(); } catch (IOException e) {System.err.println(e);} } } 

I got this error when starting the application:

 No resources specified. /D:/workspace/FileSharing_ServerSide/bin/com/Shayan/FileSharing/Server/Main.fxml:16 at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:305) at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:197) at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:588) at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707) javafx.fxml.LoadException: No resources specified. 

It says that the file does not exist, but it exists in this folder with exactly the same name! this is in the same package as the code. Does anyone know what is going on ?! thank you in advance

+6
source share
2 answers

JavaFX throws a javafx.fxml.LoadException: No resources specified. exception javafx.fxml.LoadException: No resources specified. when FXMLLoader cannot fully build a scene graph due to a missing resource.

This can happen for a variety of reasons. I came across this because of the following:

  • An error occurred while loading the controller specified in the fxml file.
  • The fxml file tries to reference the resource in the ResourceBundle , but FXMLLoader did not configure the ResourceBundle correctly.

There may be other reasons why this exception is thrown from JavaFX, but the reason is that for one reason or another, FXMLLoader encountered an exception while trying to create a scene graph from the fxml file.

+11
source

To get the resource, you need to specify the full (!) Base name. That is, with all packages before.

If the resource file has the same base name as the controller class (which is quite reasonable, as this helps bring everything together), you can do this as follows:

  String className = this.getClass().getCanonicalName(); 
  // @formatter:off ResourceBundle languageResource = ResourceBundle.getBundle(className, Locale.GERMAN); // formatter:on Object objPane = FXMLLoader.load(fxmlUrl, languageResource); 

I wrote a loader assistant for a private resource that will do the trick, getting the object and locale. Of course, I use the locale built from my settings, not a constant, but I wanted everything to be simple.

For the resource file name: since my class is called MainWindow, the resource file (in the same package) is MainWindow_de.properties (where "de" is part of the language, so I also have MainWndow_en.properties in the package.

An extension is required because this is how the file name is created. Without the extension, the file will not be recognized, which will result in your known exception.

Hope that prevents others from spending hours researching ...

+1
source

Source: https://habr.com/ru/post/948681/


All Articles