应用程序启动方法中的异常 - Javafx 控件 class
Exception in Application start method - Javafx control class
我已经阅读了所有旧问题,但我不明白发生了什么。
只有当我尝试将文件 layout.fxml 与控件 Class 连接时,问题才会出现。
我已经尝试了 1.000 条路径,但它不起作用......
我需要你的帮助,很抱歉这个愚蠢的问题:(
错误:
> 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$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: /Users/claudio/Documents/workspace/KlickiBunti/Layout.fxml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at java.net.URL.openStream(URL.java:1045)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2440)
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 control.Main_Controller.start(Main_Controller.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application control.Main_Controller
Layout.fxml Class:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="386.0" prefWidth="354.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="Controller.java">
<children>
<MenuBar AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TextArea layoutX="14.0" layoutY="41.0" prefHeight="200.0" prefWidth="200.0" promptText="hey" />
</children>
</AnchorPane>
主要Class:
package control;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
public class Main_Controller extends Application{
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
stage.setScene(new Scene(FXMLLoader.load(new File("Layout.fxml").toURI().toURL())));
stage.show();
}
}
对照Class:
package control;
public class Controller {
Controller (){
System.out.println("Try");
}
}
3 Class在同一个路径。
也许对你来说这是一个愚蠢的问题,但我尝试了 6 个小时来解决我的问题 -.-
错误消息非常明显:
Caused by: java.io.FileNotFoundException: /Users/claudio/Documents/workspace/KlickiBunti/Layout.fxml (No such file or directory).
您无论如何都应该将 FXML 文件作为资源加载:如果(当)您将应用程序捆绑为 jar 文件,FXML 无论如何都不会成为文件系统上的独立文件(即使您不这样做) , new File()
相对于您无法控制的工作目录进行解析。
如果 FXML 文件与从中加载它的 class 在同一个包中,您应该做
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("Layout.fxml"))));
如果它和控制器在同一个包里classController
,你也可以
stage.setScene(new Scene(FXMLLoader.load(Controller.class.getResource("Layout.fxml"))));
此外,fx:controller
属性需要控制器的 完全限定 classname(即 package.ClassName
),而不是 Java 源文件(由于显而易见的原因,它甚至在运行时不可用)。
我已经阅读了所有旧问题,但我不明白发生了什么。 只有当我尝试将文件 layout.fxml 与控件 Class 连接时,问题才会出现。 我已经尝试了 1.000 条路径,但它不起作用...... 我需要你的帮助,很抱歉这个愚蠢的问题:(
错误:
> 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$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: /Users/claudio/Documents/workspace/KlickiBunti/Layout.fxml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at java.net.URL.openStream(URL.java:1045)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2440)
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 control.Main_Controller.start(Main_Controller.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application control.Main_Controller
Layout.fxml Class:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="386.0" prefWidth="354.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="Controller.java">
<children>
<MenuBar AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TextArea layoutX="14.0" layoutY="41.0" prefHeight="200.0" prefWidth="200.0" promptText="hey" />
</children>
</AnchorPane>
主要Class:
package control;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
public class Main_Controller extends Application{
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
stage.setScene(new Scene(FXMLLoader.load(new File("Layout.fxml").toURI().toURL())));
stage.show();
}
}
对照Class:
package control;
public class Controller {
Controller (){
System.out.println("Try");
}
}
3 Class在同一个路径。
也许对你来说这是一个愚蠢的问题,但我尝试了 6 个小时来解决我的问题 -.-
错误消息非常明显:
Caused by: java.io.FileNotFoundException: /Users/claudio/Documents/workspace/KlickiBunti/Layout.fxml (No such file or directory).
您无论如何都应该将 FXML 文件作为资源加载:如果(当)您将应用程序捆绑为 jar 文件,FXML 无论如何都不会成为文件系统上的独立文件(即使您不这样做) , new File()
相对于您无法控制的工作目录进行解析。
如果 FXML 文件与从中加载它的 class 在同一个包中,您应该做
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("Layout.fxml"))));
如果它和控制器在同一个包里classController
,你也可以
stage.setScene(new Scene(FXMLLoader.load(Controller.class.getResource("Layout.fxml"))));
此外,fx:controller
属性需要控制器的 完全限定 classname(即 package.ClassName
),而不是 Java 源文件(由于显而易见的原因,它甚至在运行时不可用)。