0

I am trying to play a video with Java. The code I'm using is from this example.

I am using VS Code on Windows 11.

I created a new project from the command pallet using Java: Create Java Project -> JavaFX Provided by Maven for Java.

I used the Maven for Java extension to add the JavaFX Media dependency and then added requires javafx.media; in the module-info.java file.

I was able to get javafx.media module and the associated imports to resolve but get the following error when I run it:

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        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.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
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:1589)
Caused by: java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.platform.PlatformManager (in module javafx.media) cannot access class com.sun.javafx.PlatformUtil (in module javafx.base) because module javafx.base does not export com.sun.javafx to module javafx.media
        at javafx.media/com.sun.media.jfxmediaimpl.platform.PlatformManager.getSupportedProtocols(PlatformManager.java:203)
        at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.loadProtocols(NativeMediaManager.java:197)
        at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.canPlayProtocol(NativeMediaManager.java:267)
        at javafx.media/com.sun.media.jfxmedia.MediaManager.canPlayProtocol(MediaManager.java:78)
        at javafx.media/com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:239)
        at javafx.media/javafx.scene.media.Media.<init>(Media.java:393)
        at com.example/com.example.App.start(App.java:20)
        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:455)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        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
Exception running application com.example.App

module-info.java

module com.example {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.media;

    opens com.example to javafx.fxml;
    exports com.example;
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>mediaplayer</artifactId>
    <version>1</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.jfxcore</groupId>
            <artifactId>javafx-media</artifactId>
            <version>18-ea+1</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.example.App</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

App.java

package com.example;

import java.io.File;  
import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.media.Media;  
import javafx.scene.media.MediaPlayer;  
import javafx.scene.media.MediaView;  
import javafx.stage.Stage;

/**
 * JavaFX App
 */
public class App extends Application {
    @Override  
    public void start(Stage primaryStage) throws Exception {
        String path = "C:/Users/Justin/Videos/file_example_MP4_1920_18MG.mp4";

        Media media = new Media(new File(path).toURI().toString());  
          
        MediaPlayer mediaPlayer = new MediaPlayer(media);  
          
        MediaView mediaView = new MediaView(mediaPlayer);
          
        //by setting this property to true, the Video will be played   
        mediaPlayer.setAutoPlay(true);  
          
        //setting group and scene   
        Group root = new Group();  
        root.getChildren().add(mediaView);  
        Scene scene = new Scene(root,500,400);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("Playing video");  
        primaryStage.show();  
    }  
    public static void main(String[] args) {  
        launch(args);  
    }
}

I saw this answer from a related question but it wasn't helpful because I've already done what is suggested except for adding the options when running it because I am using VS Code to run it rather than running it manually from the command line.

Is there something obvious I'm doing wrong here? What do I need to do to get the video to play?

Stack Underflow
  • 2,363
  • 2
  • 26
  • 51
  • 4
    You have `org.jfxcorejavafx-media` in your Maven dependencies, instead of `org.openjfxjavafx-media`. Not sure if that is the issue, but it certainly could be. – James_D Mar 22 '23 at 19:26
  • That seems to have resolved the runtime error. The vscode Maven plugin needs updated or has a bug or something b/c that's how it generated it. Post this as an answer and I will accept it. Unfortunately, the video still is not playing, even when I add `mediaPlayer.play()` after everything else - but that's a separate problem. – Stack Underflow Mar 22 '23 at 19:34
  • 5
    You also need to ensure that all versions of JavaFX libraries you use are the same (the media library is at `18-ea+1` and other libraries are at `13`). The different versions aren't necessarily compatible with each other. And the `ea` versions are early access versions that haven't undergone the additional QA and stabilization that the non-ea versions have. It is best to always use the latest stable version if you can (those will usually have the most bugs, security flaws, and features addressed in them). For instance, JavaFX 13 won't work on modern M1 macs, but 19+ will. – jewelsea Mar 22 '23 at 22:02

1 Answers1

4

Your pom.xml has

<groupId>org.jfxcore</groupId>
<artifactId>javafx-media</artifactId>

for the javafx.media dependency. The group id should be org.openjfx:

<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>

Also make sure all JavaFX dependencies use the same version.

James_D
  • 201,275
  • 16
  • 291
  • 322