项目场景:
在javafx框架下,在使用Image类通过URL加载视频的时候,出现加载错误。
问题描述
Media media = new Media(URL_MEDIA);
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mediaView = new MediaView(mediaPlayer);
以上代码引起的如下报错:
cpp
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1147)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:1570)
Caused by: java.lang.IllegalAccessError: class com.sun.media.jfxmedia.locator.Locator (in unnamed module @0x188b3d43) cannot access class com.sun.javafx.PlatformUtil (in module javafx.base) because module javafx.base does not export com.sun.javafx to unnamed module @0x188b3d43
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:235)
at javafx.scene.media.Media.<init>(Media.java:392)
at MyMedia.start(MyMedia.java:25)
at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@22.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@22.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@22.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at javafx.graphics@22.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at javafx.graphics@22.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@22.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application MyMedia
原因分析:
开始以为是路径原因造成的无法找到视频文件,所以加了throw exception来看,但是发现不是这个问题,是缺少javafx.media造成的无法引用media模块。
解决方案:
最终在launch.json中加上了下属的media模块
cpp
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "MyMedia",
"request": "launch",
"mainClass": "MyMedia",
"projectName": "java_d2184a8b",
"vmArgs": "--module-path lib/ --add-modules=javafx.controls,javafx.fxml,javafx.media"
},