加载视频文件出现错误

项目场景:

在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"
        },
相关推荐
IceBing3 分钟前
还在一个个连接 Arthas?这个开源平台支持批量诊断 JVM
java
SL_staff9 分钟前
《如何用规则引擎替代if-else?JVS-Rules可视化编排比硬编码强在哪里?》
java·低代码·架构
Sam_Deep_Thinking18 分钟前
java中的class到底是个什么东西?
java·开发语言·面试
swordbob21 分钟前
Spring 3 级缓存解决循环依赖
java·spring
摇滚侠22 分钟前
SpringMVC 入门到实战 获取请求参数 25-32
java·spring·intellij-idea
咖啡八杯23 分钟前
【无标题】
java·后端·设计模式
mqiqe24 分钟前
面试题-MyBatis 面试篇
java·面试·mybatis
摇滚侠25 分钟前
SpringMVC 入门到实战 @RequestMapping 14-24
java·spring
云烟成雨TD27 分钟前
Spring AI Alibaba 1.x 系列【80】可观测集成
java·人工智能·spring
Filwaod1 小时前
MCP 接入模式对比:Agent - Gateway - 业务项目 vs Agent - Adapter - 业务项目
java·agent·mcp