Java报错Exception in Application start method

问题

使用JavaFX Scene Builder时,主程序报错:找不到文件(应该是LoginUIController.java等文件)
恳请朋友们来指定迷津T^T

我的情况

  • 系统:MacOS Monterey
  • 编译器:Eclipse
  • Java版本:Java1.8
  • JavaFX Scene Builder2.0

报错信息

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$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoClassDefFoundError: SystemGUI/LoginUIController (wrong name: systemGUI/LoginUIController)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    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.load(FXMLLoader.java:2409)
    at mainSystem.Main.showLoginView(Main.java:27)
    at mainSystem.Main.start(Main.java:20)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application mainSystem.Main

主程序代码


package mainSystem;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import systemGUI.LoginUIController;
import java.io.IOException;


public class Main extends Application{
    private Stage primaryStage;
    private User userP;
    
    @Override
    public void start(Stage primaryStage) throws Exception{
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("健身管理系统");
        showLoginView();
    }

    public void showLoginView() {
        try {
            // 加载登录页面
            FXMLLoader loader = new FXMLLoader(Main.class.getResource("/systemGUI/LoginUI.fxml"));
            Parent root = loader.load();
            LoginUIController loginUIController = loader.getController();
            primaryStage.setScene(new Scene(root, 1440, 900));
            primaryStage.show();
           
            // 连接上控制器
            System.out.println(loginUIController);
            loginUIController.setMainApp(this);
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    public void setUserP(User user) {
        this.userP = user;
    }

    public User getUserP() {
        return userP;
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

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

}

LoginUI.fxml代码

xml version="1.0" encoding="UTF-8"?>

import java.net.*?>
import javafx.scene.paint.*?>
import javafx.scene.text.*?>
import javafx.scene.control.*?>
import javafx.geometry.*?>
import java.lang.*?>
import javafx.scene.layout.*?>

<fx:root fx:id="myStackPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" opacity="0.99" prefHeight="900.0" prefWidth="1440.0" styleClass="bg" stylesheets="@../mycss/login.css" type="StackPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SystemGUI.LoginUIController">
   <children>
      <VBox fx:id="signUpBox" alignment="CENTER" prefHeight="200.0" prefWidth="100.0">
         <StackPane.margin>
            <Insets bottom="160.0" left="510.0" right="510.0" top="160.0" />
         StackPane.margin>
         <padding>
            <Insets left="30.0" right="30.0" />
         padding>
         <children>
            <Label text="注册" textAlignment="CENTER" textFill="#40514e">
               <font>
                  <Font size="36.0" />
               font>
               <VBox.margin>
                  <Insets top="40.0" />
               VBox.margin>
            Label>
            <TextField fx:id="userName" promptText="用户名" styleClass="inputText">
               <VBox.margin>
                  <Insets top="20.0" />
               VBox.margin>
               <font>
                  <Font size="18.0" />
               font>
            TextField>
            <TextField fx:id="userPassword" promptText="密码" styleClass="inputText">
               <font>
                  <Font size="18.0" />
               font>
               <VBox.margin>
                  <Insets top="20.0" />
               VBox.margin>
            TextField>
            <TextField fx:id="userSex" promptText="性别" styleClass="inputText">
               <font>
                  <Font size="18.0" />
               font>
               <VBox.margin>
                  <Insets top="20.0" />
               VBox.margin>
            TextField>
            <TextField fx:id="userAge" promptText="年龄" styleClass="inputText">
               <VBox.margin>
                  <Insets top="20.0" />
               VBox.margin>
               <font>
                  <Font size="18.0" />
               font>
            TextField>
            <TextField fx:id="userHeight" promptText="身高" styleClass="inputText">
               <font>
                  <Font size="18.0" />
               font>
               <VBox.margin>
                  <Insets top="20.0" />
               VBox.margin>
            TextField>
            <TextField fx:id="userWeight" promptText="体重" styleClass="inputText">
               <font>
                  <Font size="18.0" />
               font>
               <VBox.margin>
                  <Insets top="20.0" />
               VBox.margin>
            TextField>
            <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="8.0">
               <VBox.margin>
                  <Insets bottom="40.0" top="18.0" />
               VBox.margin>
               <children>
                  <Button fx:id="saveButton" mnemonicParsing="false" onAction="#handleNewUser" styleClass="SigninBG" text="注册" textFill="WHITE">
                     <font>
                        <Font size="22.0" />
                     font>
                  Button>
                  <Button fx:id="backButton" mnemonicParsing="false" onAction="#handleBack" text="返回登录" textFill="WHITE">
                     <HBox.margin>
                        <Insets left="6.0" />
                     HBox.margin>
                     <font>
                        <Font size="22.0" />
                     font>
                  Button>
               children>
            HBox>
            <Text fx:id="hintSignUp" fill="#5362e4" strokeType="OUTSIDE" strokeWidth="0.0">
               <font>
                  <Font size="17.0" />
               font>
               <VBox.margin>
                  <Insets bottom="20.0" />
               VBox.margin>
            Text>
         children>
      VBox>
      <VBox fx:id="loginBox" alignment="CENTER" prefHeight="200.0" prefWidth="100.0">
         <StackPane.margin>
            <Insets bottom="186.0" left="510.0" right="510.0" top="186.0" />
         StackPane.margin>
         <padding>
            <Insets left="26.0" right="26.0" top="30.0" />
         padding>
         <children>
            <Label text="健身管理系统" textFill="#40514e">
               <font>
                  <Font size="36.0" />
               font>
               <VBox.margin>
                  <Insets top="10.0" />
               VBox.margin>
            Label>
            <TextField fx:id="userNameF" promptText="请输入用户名" styleClass="inputText" text="邓富丽">
               <VBox.margin>
                  <Insets top="30.0" />
               VBox.margin>
               <font>
                  <Font size="18.0" />
               font>
            TextField>
            <PasswordField fx:id="passwordF" promptText="请输入密码" styleClass="inputText" text="12345">
               <font>
                  <Font size="18.0" />
               font>
               <VBox.margin>
                  <Insets top="20.0" />
               VBox.margin>
            PasswordField>
            <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="8.0">
               <children>
                  <Button fx:id="loginButton" mnemonicParsing="false" onAction="#handleLogin" style="-fx-background-color: #30E3CA;" text="登录" textFill="WHITE">
                     <font>
                        <Font size="22.0" />
                     font>
                  Button>
                  <Button fx:id="signUpButton" mnemonicParsing="false" onAction="#handleSignUp" style="-fx-background-color: #40514E;" text="注册" textFill="WHITE">
                     <font>
                        <Font size="22.0" />
                     font>
                  Button>
               children>
               <VBox.margin>
                  <Insets top="30.0" />
               VBox.margin>
            HBox>
            <Text fx:id="hint" fill="#5362e4" strokeType="OUTSIDE" strokeWidth="0.0">
               <font>
                  <Font size="17.0" />
               font>
               <VBox.margin>
                  <Insets />
               VBox.margin>
            Text>
         children>
      VBox>
   children>
fx:root>

LoginUIController 代码

package systemGUI;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import mainSystem.Main;        // 导入主程序

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class LoginUIController {
    @FXML
    private TextField userSex;
    @FXML
    private VBox signUpBox;
    @FXML
    private TextField userPassword;
    @FXML
    private TextField userWeight;
    @FXML
    private TextField userHeight;
    @FXML
    private PasswordField passwordF;
    @FXML
    private TextField userNameF;
    @FXML
    private TextField userName;
    @FXML
    private VBox loginBox;
    @FXML
    private TextField userAge;
    @FXML
    private Text hint;
    @FXML
    private Text hintSignUp;
    @FXML
    private Button loginButton;
    @FXML
    private Button signUpButton;
    @FXML
    private Button backButton;
    @FXML
    private Button saveButton;
    
    @FXML
    private Main mainApp;
    public void setMainApp(Main mainApp) {
        this.mainApp = mainApp;
    }

    public Main getMainApp() {
        return mainApp;
    }

    @FXML
    private void handleLogin() throws Exception {        // 登录按键
        // 调试用信息,之后可去掉
        System.out.println("登录按钮被点击了");
        
        boolean result = login(userNameF.getText(),passwordF.getText());
            if(result) {
                try {
                    FXMLLoader loader = new FXMLLoader(Main.class.getResource("systemGUI/MainUI.fxml"));
                    Parent newView = loader.load();
                    MainUIController mainUIController = loader.getController();
                    System.out.println("mainApp: " + mainApp);
                    mainUIController.setMainApp(mainApp);
                    mainApp.getPrimaryStage().setScene(new Scene(newView, 1440, 900));
                }catch(Exception e1) {
                    e1.printStackTrace();
                }
            }
            else
                hint.setText("登录失败,请检查输入是否正确。");
    }
    
    boolean login(String user, String pwd) {
        boolean result = false;
        File file = new File("userAccount.txt");
        Scanner input;
        try {
            input = new Scanner(file);
            while(input.hasNext()) {
                String line = input.nextLine();
                String[] fields = line.split(",");
                if(user.equals(fields[0]) && pwd.equals(fields[1])) {
                    result = true;
                    break;
            }
        }
        input.close();
     }
     catch(FileNotFoundException e) {
        e.printStackTrace();
     }
     return result;
   }
      
    
    @FXML
    private void handleSignUp() {
        //调试提示
        System.out.println("注册按钮被点击了");
        // 切换两个页面的显示
        loginBox.setVisible(false);
        signUpBox.setVisible(true);
    }
    
    @FXML
    private void handleNewUser() {
        String str;
        str = userName.getText() + " " + userPassword.getText() + " " + userSex.getText() + " " + userAge.getText() + " " + userHeight.getText() + " " + userWeight.getText();
        // 文件追加数据
        try {
            File copy = new File("copy.txt");
            BufferedWriter c_w = new BufferedWriter(new FileWriter("copy.txt"));
            BufferedReader uA_r = new BufferedReader(new FileReader("userAccount.txt"));
            String uA_copy;
            while((uA_copy = uA_r.readLine())!= null) {
                c_w.write(uA_copy);
                c_w.write("\n");
            }
            c_w.close();
            BufferedReader c_r = new BufferedReader(new FileReader("copy.txt"));
            String c_str;
            BufferedWriter uA_w = new BufferedWriter(new FileWriter("userAccount.txt"));
            while((c_str = c_r.readLine()) != null) {
                uA_w.write(c_str);
                uA_w.write("\n");
            }
            uA_w.write(str);
            uA_w.close();
            uA_r.close();
            c_r.close();
            c_w.close();
            copy.delete();
        }catch(Exception e) {
            e.printStackTrace();
        }
        hintSignUp.setText("注册成功");
        
        // 注册完成,跳转登录
        loginBox.setVisible(true);
        signUpBox.setVisible(false);
       
        
    }
    @FXML
    private void handleBack() {
        signUpBox.setVisible(false);
        loginBox.setVisible(true);
    }
}

文件逻辑

img

刚刚参考知乎的一篇文章JavaFx的那些坑--学习JavaFX过程中遇到的坑一 - 知乎 ,感觉你的代码可能有两个地方有问题,当然我也没有用过,这里仅做参考:
1.LoginUI.fxml的这个属性 fx:controller="SystemGUI.LoginUIController" ,应该是小写的“systemGUI”才对,因为你的包命名就是小写的;
2. FXMLLoader loader = new FXMLLoader(Main.class.getResource("systemGUI/MainUI.fxml")); 这行代码我觉得应该这样
FXMLLoader loader = new FXMLLoader(SystemGUI.LoginUIController.class.getResource("MainUI.fxml"));