JavaFX使用Image对象报错求助(含代码及错误信息)
JavaFX Image加载报错解决方案
问题场景
作为JavaFX初学者,运行代码时只要添加Image对象就会报错,注释掉相关代码后程序可正常运行。
代码示例
package com.example.checking; import javafx.application.Application; import javafx.scene.image.Image; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; public class HelloApplication extends Application{ //Main method to run program public static void main(String[] args) { //to launch the application launch(args); } @Override public void start(Stage stage) throws Exception { //Now we need a scene inside stage and root nodes. Group root = new Group(); Scene scene = new Scene(root, 600,600, Color.LIGHTBLUE); //Creating and Displaying icon. Image i = new Image("icon.png"); stage.getIcons().add(i); Text text = new Text(); text.setText("Whoaaaa!!!"); text.setX(50); text.setY(50); //Adding text to our root node text.setFont(Font.font("Verdana", 50)); text.setFill(Color.LIMEGREEN); //Setting title Line line = new Line(); line.setStartX(200); line.setStartY(200); line.setEndX(500); line.setEndY(200); line.setStrokeWidth(5); Rectangle rectangle = new Rectangle(); rectangle.setWidth(100); rectangle.setHeight(100); rectangle.setX(300); rectangle.setY(250); rectangle.setStrokeWidth(5); rectangle.setFill(Color.BLUE); rectangle.setStrokeWidth(5); rectangle.setStroke(Color.BLACK); Polygon triangle = new Polygon(); triangle.getPoints().setAll( 200.0, 200.0, 300.0, 300.0, 200.0, 300.0, 300.0, 200.0 ); triangle.setFill(Color.YELLOW); Circle circle = new Circle(); circle.setCenterX(350); circle.setCenterY(350); circle.setRadius(50); circle.setFill(Color.ORANGE); root.getChildren().add(text); root.getChildren().add(line); root.getChildren().add(triangle); root.getChildren().add(rectangle); root.getChildren().add(circle); stage.setTitle("Notes and Password Manager"); //Setting stage height and width // stage.setHeight(750); // stage.setWidth(630); //Set the scene so it shows in the stage. stage.setScene(scene); stage.setResizable(false); //To show the stage we use .show() stage.show(); } }
报错信息
Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) 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.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1082) 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.IllegalArgumentException: Invalid URL: Invalid URL or resource not found at javafx.graphics@19/javafx.scene.image.Image.validateUrl(Image.java:1138) at javafx.graphics@19/javafx.scene.image.Image.<init>(Image.java:628) at com.example.checking.HelloApplication.start(HelloApplication.java:76) 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 Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at javafx.graphics@19/javafx.scene.image.Image.validateUrl(Image.java:1123) ... 11 more Exception running application com.example.checking.HelloApplication
已尝试的操作
- 创建
module-info.java并添加require javafx.controls; require javafx.fxml;,但出现错误:Error occurred during initialization of boot layer java.lang.module.FindException: Module Checking not found - 删除
module-info.java,改用VM选项配置:--module-path "C:\Users\jaina\Documents\openjfx-19_windows-x64_bin-sdk (1)\javafx-sdk-19\lib" --add-modules javafx.controls,javafx.fxml,配置后其他功能正常,但Image对象仍报错
解决方案
1. 正确放置图片资源
JavaFX加载资源时,直接使用文件名"icon.png"会从类路径根目录查找文件。需要将icon.png放在项目的类路径下:
- 若为Maven/Gradle项目,放在
src/main/resources目录下 - 若为普通Java项目,放在
src目录下(编译后会自动复制到输出目录的类路径中)
2. 使用类加载器获取资源URL
如果资源路径正确但仍加载失败,建议通过类加载器获取资源的绝对URL,避免路径解析问题:
// 替换原有的Image初始化代码 Image i = new Image(getClass().getResource("/icon.png").toExternalForm()); stage.getIcons().add(i);
注意:/icon.png中的斜杠表示类路径的根目录,需确保图片确实存在于该位置。
3. 检查VM参数(确保依赖完整)
Image类属于javafx.graphics模块,而javafx.controls已依赖javafx.graphics,所以当前VM参数无需额外添加,但需确认路径正确:
--module-path "你的JavaFX SDK路径/lib" --add-modules javafx.controls,javafx.fxml
4. 修复module-info问题(可选)
若想重新使用模块系统,需确保module-info.java的模块名与项目包结构匹配,例如主类在com.example.checking包下,模块名设为com.example.checking:
module com.example.checking { requires javafx.controls; requires javafx.fxml; opens com.example.checking to javafx.fxml; exports com.example.checking; }
同时确保VM参数中的模块路径正确,运行时能找到该模块。
内容的提问来源于stack exchange,提问作者Krattza
相关产品推荐
相关产品推荐

