启动方法异常,MainController ClassNotFound

时间:2018-12-05 18:17:14

标签: java user-interface javafx fxml

我有一些问题。我添加了GUI,但无法运行我的应用程序。我试图以其他方式设置源,但没有帮助。

开始课程:

public class Main extends Application
{
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("gui/Main.fxml"));

    Scene scene = new Scene(root);
    stage.setMinHeight(600);
    stage.setMinWidth(800);
    stage.setScene(scene);
    stage.show();
}

我正在尝试:

("/gui/Main.fxml"); 
("Main.fxml"); 
("/Main.fxml"); 
("gui/Main") etc.

主类:

 public static void main(String[] args) throws SQLException, IOException {

        String databaseUrl = "jdbc:sqlite:bazadanych.db";

        ConnectionSource connectionSource;
        connectionSource = new JdbcConnectionSource(databaseUrl);


        Dao<Klient, Integer> klientsDao =
                DaoManager.createDao(connectionSource, Klient.class);
        Dao<Samochod,Integer> samochodsDao =
                DaoManager.createDao(connectionSource, Samochod.class);
        Dao<Usterka,Integer> usterkasDao =
                DaoManager.createDao(connectionSource, Usterka.class);

        launch(args);


        connectionSource.close();
    }

FXML文件的第一行:

<AnchorPane minHeight="600.0" minWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui/MainController">

控制器:

package gui;

public class MainController implements Initializable {


    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

}

错误:

"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=55783:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;C:\Users\Kowali\Desktop\Projekt0-2\Projekt\target\classes;C:\Users\Kowali\.m2\repository\com\j256\ormlite\ormlite-jdbc\5.1\ormlite-jdbc-5.1.jar;C:\Users\Kowali\.m2\repository\com\j256\ormlite\ormlite-core\5.1\ormlite-core-5.1.jar;C:\Users\Kowali\.m2\repository\org\xerial\sqlite-jdbc\3.25.2\sqlite-jdbc-3.25.2.jar" Main
2018-12-04 16:10:59,239 [DEBUG] DaoManager created dao for class class Klient with reflection
2018-12-04 16:10:59,241 [DEBUG] DaoManager created dao for class class Samochod with reflection
2018-12-04 16:10:59,245 [DEBUG] DaoManager created dao for class class Usterka with reflection
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Kowali/Desktop/Projekt0-2/Projekt/target/classes/Main.fxml:10
    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    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 Main.start(Main.java:18)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: MainController
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    ... 22 more
Exception running application Main

Process finished with exit code 1

我正在研究Intelij,所以我将FXML和Controller移到了资源上,但是它也帮不了我。我也阅读了有关类似问题的一些答复,但它们并没有帮助我。我也尝试使用FXML Loader,而不是父级root。我也不明白为什么在Intelij的Controller中可以正常运行,但是在错误的结尾我们可以看到找不到它。我整天都在寻找解决方案。

我也在尝试这样做:

FXMLLoader loader = FXMLLoader.load(getClass().getResource("Main.fxml"));
Parent root = loader.load();

1 个答案:

答案 0 :(得分:0)

正在加载的文件为C:/Users/Kowali/Desktop/Projekt0-2/Projekt/target/classes/Main.fxml。它可能是旧文件。建议您删除该文件,在IntelliJ中执行Project-> Rebuild project,然后尝试再次运行。

您还声称您的代码是

Parent root = FXMLLoader.load(getClass().getResource("gui/Main.fxml"));

,但这与错误消息不一致。因此,要么您没有保存.java文件(但是可能不是这样,因为您正在使用IntelliJ,它会自动保存文件),或者这不是与错误消息匹配的代码版本。