如何在JavaFX上为本地项目设置路径

时间:2017-07-15 07:16:55

标签: java javafx fxml

我想创建一个本地JavaFX应用程序,但我不知道如何设置文件的路径,因此当我构建jar时,我不会遇到任何问题。

我已经制作了这个jar并试图修复尽可能多的错误,但我还有一些我无法修复的错误。

这是源文件夹的pic

这些是我的错误

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
file:/D:/Users/eebal/IdeaProjects/Recetario/out/artifacts/Recetario/Recetario.jar!/sample/home.fxml

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
        at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
        at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at sample.Main.start(Main.java:21)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more
Caused by: java.lang.ClassNotFoundException: com.jfoenix.controls.JFXButton
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
        at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
        at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
        ... 20 more
Exception running application sample.Main

D:\Users\eebal\IdeaProjects\Recetario\out\artifacts\Recetario>java -jar Recetario.jar
Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
file:/D:/Users/eebal/IdeaProjects/Recetario/out/artifacts/Recetario/Recetario.jar!/sample/home.fxml

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
        at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
        at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at sample.Main.start(Main.java:21)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more
Caused by: java.lang.ClassNotFoundException: com.jfoenix.controls.JFXButton
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
        at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
        at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
        ... 20 more
Exception running application sample.Main

这是Main Parent home = FXMLLoader.load(getClass().getResource("home.fxml"));

的第21行

1 个答案:

答案 0 :(得分:0)

您的错误日志显示Caused by: java.lang.ClassNotFoundException: com.jfoenix.controls.JFXButton。这意味着无法加载fxml,因为找不到类JFXButton。我认为您的库导入jfoenix存在问题,或者您忘记在您的FXML文件中包含JFXButton导入。