这是在start方法中的一段代码。其中,logo.jpg这个文件是真实存在的。
而我对这个文件分别使用了绝对路径和相对路径进行测试。每一种路径在测试时都抛出了InvocationTargetException异常
这个异常翻译过来就是调用目标异常。本来我是使用图片的相对路径的,发生异常后我就改成了绝对路径,可是任然抛出了这个异常
这是IDEA在debug模式下运行代码时控制台的输出:
"C:\Program Files\Java\jdk-16.0.1\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:4462,suspend=y,server=n -javaagent:C:\Users\ACE\AppData\Local\JetBrains\IdeaIC2021.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\Users\ACE\.m2\repository\org\openjfx\javafx-controls\16\javafx-controls-16.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-graphics\16\javafx-graphics-16.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-base\16\javafx-base-16.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-fxml\16\javafx-fxml-16.jar;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.3\lib\idea_rt.jar" -p C:\Users\ACE\.m2\repository\org\openjfx\javafx-fxml\16\javafx-fxml-16-win.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-base\16\javafx-base-16-win.jar;C:\Users\ACE\.m2\repository\org\openjfx\javafx-graphics\16\javafx-graphics-16-win.jar;F:\JavafxStudyDemo02\target\classes;C:\Users\ACE\.m2\repository\org\openjfx\javafx-controls\16\javafx-controls-16-win.jar -m com.example.javafxstudydemo02/com.example.javafxstudydemo02.HelloApplication
已连接到目标 VM, 地址: ''127.0.0.1:4462',传输: '套接字''
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
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:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1125)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:618)
at com.example.javafxstudydemo02/com.example.javafxstudydemo02.HelloApplication.start(HelloApplication.java:49)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run$$$capture(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1117)
... 12 more
Exception running application com.example.javafxstudydemo02.HelloApplication
与目标 VM 断开连接, 地址为: ''127.0.0.1:4462',传输: '套接字''
进程已结束,退出代码为 1
这是HelloApplication文件源码:
package com.example.javafxstudydemo02;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class HelloApplication extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception
{
Button button = new Button("百度");
BorderPane pane = new BorderPane(button);
Scene scene = new Scene(pane,500,500);
button.setOnAction(e -> {
getHostServices().showDocument("https://www.baidu.com/");
});
stage.setTitle("FirstJavafxApp");
stage.setScene(scene);//将Scene场景添加到stage里
stage.getIcons().add(new Image("src/main/resources/image/logo.jpg"));
//getIcons方法获取应用图标,通过add方法添加一个Image对象。该对象相对路径在src/main/resources/image/logo.jpg
stage.show();
}
}
错误应该就是在设置应用图标的那一行,但是我不明白,为什么我使用绝对路径和相对路径都发生了错误。求解答,谢谢