7

I'm new to javaFX and I'm trying to run a simple app. it's UI is created with javaFX scenebuilder and the Main class is supposed to display the UI, nothin else.

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 running the app:

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 doesn't exists, but it exists in that folder with the exact same name! it's in the same package as the code is. anybody knows what's going on?! thanks in advance

3
  • 2
    what is the content of Main.fxml? It looks like the error is in there. Commented Jul 4, 2013 at 7:24
  • Main.fxml are correcty in the same package as your main class ? Commented Jul 4, 2013 at 8:17
  • Yes, the error was from fxml file!! the name of the controller was not correctly specified in fxml code. thanks Dahaka ;) Commented Jul 4, 2013 at 8:20

2 Answers 2

13

JavaFX throws the exception javafx.fxml.LoadException: No resources specified. when the FXMLLoader could not fully build the scene graph because of a missing resource.

This could happen for a variety reasons. I've encountered it because of the following:

  1. There was an error loading the controller specified in the fxml file.
  2. The fxml file tries to reference a resource in a ResourceBundle but the FXMLLoader did not have the ResourceBundle properly configured.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Emphasis on your 2. reason. Took me forever to realize. Thanks!
1

To get the resource, you'll have to specify the full (!) base name. That is, with all the packages before.

If the resource file has the same bas name as the controller class (which is quite reasonable as it helps keeping things together), you can do this in the following way:

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

    Object objPane = FXMLLoader.load(fxmlUrl, languageResource);

I have written a private resource loader helper, that will do the trick by getting an Object and a Locale. Of course, I use a locale constructed from my settings, not a constant, but I wanted to keep things simple.

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

The extension is required, as this is the way the file name is being constructed. Without the extension, the file will not be recognized, leading to your well-known exception.

Hope that prevents others from spending hours in doing research...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.