You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX媒体播放器开发:无法定位terre.mp4文件求助

JavaFX媒体播放器getResource返回null,无法找到同目录视频文件

问题场景

我正在完成学校的JavaFX媒体播放器开发练习,操作步骤及遇到的问题如下:

  1. 通过Scene Builder创建了包含MediaView的BorderPane,生成的primary.fxml代码:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.media.MediaView?>


<BorderPane prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="com.guigui.testvideo.PrimaryController">
   <center>
      <MediaView fx:id="mediaview" fitHeight="200.0" fitWidth="200.0" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>
  1. 在PrimaryController中编写播放逻辑,尝试加载同目录的terre.mp4:
package com.guigui.testvideo;

import java.io.IOException;
import javafx.fxml.FXML;

import javafx.scene.media.MediaView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;


import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;


public class PrimaryController implements Initializable {
    
    @FXML private MediaView mediaview;
    MediaPlayer mediaplayer;
    
    @Override
    public void initialize(URL url, ResourceBundle rb){
    URL myUrl = PrimaryController.class.getResource("terre.mp4");
    Media media = new Media(myUrl.toExternalForm());
    mediaplayer = new MediaPlayer(media);  
    
    mediaview.setMediaPlayer(mediaplayer);
    mediaplayer.play();
    
    }

}
  1. NetBeans未检测到语法错误,但运行时Maven报错:
Failed to execute goal org.openjfx:javafx-maven-plugin:0.0.4:run (default-cli) on project TestVideo: Error: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
  1. 调试发现getResource("terre.mp4")返回null,视频文件和PrimaryController.java在同一目录下,调整过目录和路径都无法解决。

解决方案

1. 配置Maven资源过滤,确保视频文件被打包到类路径

Maven默认仅将src/main/resources下的文件打包到类路径,若视频放在src/main/java的包目录下,需修改pom.xml让Maven包含非Java文件:

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.mp4</include>
                <include>**/*.fxml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.4</version>
            <configuration>
                <mainClass>com.guigui.testvideo.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

2. 检查文件位置与编译后路径

  • 确认terre.mp4确实在src/main/java/com/guigui/testvideo目录下,文件名大小写与代码一致(Linux/macOS区分大小写)。
  • 编译后查看target/classes/com/guigui/testvideo目录,确认terre.mp4已被复制到此,若未出现则检查Maven资源配置是否生效。

3. 改用类加载器加载资源(备选)

若上述配置无效,可尝试从根类路径开始查找:

URL myUrl = getClass().getClassLoader().getResource("com/guigui/testvideo/terre.mp4");

4. 用绝对路径临时调试(仅排查逻辑)

为验证播放逻辑是否正常,可临时使用绝对路径加载:

import java.io.File;

// ...

File videoFile = new File("src/main/java/com/guigui/testvideo/terre.mp4");
Media media = new Media(videoFile.toURI().toString());

注意:绝对路径仅用于本地调试,发布时需换回类路径加载方式。

5. 清理并重新构建项目

执行NetBeans的Clean and Build,或命令行运行:

mvn clean install

清空旧编译文件,重新打包所有资源,避免缓存干扰。

内容的提问来源于stack exchange,提问作者guillaume belanger methot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 10:05:18