0

I need some help from you.. I have created sample javafx app just to check something.

HelloApplication

package com.example.demo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch();
    }
}

HelloController

package com.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
    @FXML
    private Label welcomeText;
    @FXML
    protected void onHelloButtonClick() {
        welcomeText.setText("Welcome to JavaFX Application!");
    }
}

When i compile and launch my application from IDE it's everything ok. The problem start when I try to run it from command line. Firstable compile:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml HelloApplication.java

Then trying to run:

java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml com.example.demo.HelloApplication

i got some errors:

> Exception in Application start method
java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
        at java.base/java.lang.reflect.Method.invoke(Method.java:577)
        at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
        at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:577)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
        at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
        at javafx.fxml@19/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
        at com.example.demo.HelloApplication.start(HelloApplication.java:14)
        at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
        at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
        at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
        at javafx.graphics@19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at javafx.graphics@19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics@19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
        ... 1 more
Exception running application com.example.demo.HelloApplication

I dont know why do i get these errors. I am 100% sure that path to java fx lib is correct. I have also problem to open apps from jar/exe files. I didnt change anything, but just trying to run it as javafx generate it.

@Edit It doesnt work either: FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/demo/hello-view.fxml")); I dont know why, but this doesnt solve my problem.

merridius
  • 1
  • 1
  • Possible duplicate of [_How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?_](https://stackoverflow.com/q/61531317/230513), [_et al_.](https://stackoverflow.com/search?q=%5Bjavafx%5D+fxml+Location+is+not+set) – trashgod Oct 18 '22 at 17:15
  • With the setup you have, `hello-view.fxml` should be in `com/example/demo`, relative to the folder you are running from in the command line. (I.e. it should be in the same place as `HelloApplication.class`.) It looks like it is not there. – James_D Oct 18 '22 at 18:01
  • It doesnt work either: FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/demo/hello-view.fxml")); I dont know why, but this doesnt solve my problem – merridius Oct 18 '22 at 21:01
  • See _leading /_, discussed [here](https://stackoverflow.com/q/61531317/230513). – trashgod Oct 18 '22 at 22:32
  • Is the FXML file in the expected place (in the same directory as `HelloApplication.class`)? – James_D Oct 19 '22 at 10:36

0 Answers0