Search code examples
javafxml

java.util.MissingResourceException: write old way and old bundle name


Fxml files sees. Put an FXML folder with files todolist.xml, todolistedit.xml (folder resources), write:

FXMLLoader.setLocation(getClass().getResource("/fxml/todolist.fxml"));

clean -> package -> FXML files appeared in the target.

My name bundle TestBundle

He's looking for some old way, I've already changed it.

Old way and old bundle name: ru.todolist.javafx.bundles.Locale

Text error:

Caused by: java.util.MissingResourceException: 
Can't find bundle for base name ru.todolist.javafx.bundles.Locale, locale ru.

Assembly folder:

enter image description here

class Main:

    public class Main extends Application {

    private static Locale DEFAULT_LOCALE = new Locale("en");

    @Override
    public void start(Stage stage) throws Exception {

        Locale.setDefault(DEFAULT_LOCALE);

        FXMLLoader fxmlLoader = new FXMLLoader();

     //   fxmlLoader.setLocation(getClass().getResource("../fxml/todolist.fxml"));
        fxmlLoader.setLocation(getClass().getResource("/fxml/todolist.fxml"));
        fxmlLoader.setResources(ResourceBundle.getBundle("TestBundle", new Locale("ru")));

        Parent fxmlMain = fxmlLoader.load();
        MainController mainController = fxmlLoader.getController();
        mainController.setMainStage(stage);

        stage.setTitle(fxmlLoader.getResources().getString("todo_list"));
        stage.setMinHeight(650);
        stage.setMinWidth(750);
        stage.setScene(new Scene(fxmlMain, 650, 675));
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Program hierarchy:

enter image description here

I tried the options, moved the resource to the folder and where is the whole project in general:

fxmlLoader.setResources(ResourceBundle.getBundle("TestBundle"));; //or
fxmlLoader.setResources(ResourceBundle.getBundle("TestBundle_ru"));;// or 
fxmlLoader.setResources(ResourceBundle.getBundle("TestBundle", new Locale("ru"))); // or

full error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException: 
/C:/Users/den/IdeaProjects/TodoListFx/TodoListFx/target/classes/fxml/todolist.fxml

    at [email protected]/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at [email protected]/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at ru.todolist.javafx.start.Main.start(Main.java:28)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Caused by: java.util.MissingResourceException: Can't find bundle for base name ru.todolist.javafx.bundles.Locale, locale ru
    at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1556)
    at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:932)
    at ru.todolist.javafx.controllers.MainController.initialize(MainController.java:128)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
    ... 12 more
Exception running application ru.todolist.javafx.start.Main

Solution

  • Solved.It was necessary to register in the second controller that in the first: it was:

    fxmlLoader.setLocation(getClass().getResource("../fxml/todolistEdit.fxml"));
            fxmlLoader.setResources(ResourceBundle.getBundle("ru.todolist.javafx.bundles.Locale", new Locale("ru")));
    

    need:

    fxmlLoader.setLocation(getClass().getResource("/fxml/todolistEdit.fxml"));
                fxmlLoader.setResources(ResourceBundle.getBundle("TestBundle", new Locale("ru")));