-1

I have been working on this project for some time now and can not figure out why the Media class causes my application to crash on JavaFX(NetBeans).

After watching several tutorials on how to use the Media class, I came up with this:

class CoinFall extends TimerTask {

    Pane pane;
    
    public void run() {
        Platform.runLater(()-> {
            CTimer animation=new CTimer();
            animation.setUP(pane);
            animation.start();
        });
    }
    
    public void setUP(Pane p) {
        pane=p;
    }
}

public class NewFXMain extends Application {
    Media media=new Media(new File("C:\\ProjectImages\\baby.mp3").toURI().toString());
    
    @Override
    public void start(Stage primaryStage) {
        final int SCENE_WIDTH=600, SCENE_HIGHT=600;
        final double GRAVITY=200, FORCE=200;
        Pane canvas=new Pane();
        Group root = new Group();
        
        Circle center = new Circle();
        center.setCenterX(300.0);
        center.setCenterY(300.0);
        center.setRadius(10.0);
        
        Line ground=new Line();
        ground.setStartX(0);
        ground.setStartY((SCENE_HIGHT/20)*19);
        ground.setEndX(SCENE_WIDTH);
        ground.setEndY((SCENE_HIGHT/20)*19);
        
        Button fireB=new Button();
        fireB.setLayoutX(450);
        fireB.setLayoutY(5);
        fireB.setText("Fire!");
        
        CoinFall cTask=new CoinFall();
        cTask.setUP(canvas);
        Timer cTimer=new Timer();
        cTimer.schedule(cTask, 0, 1000);
        
        CannonFall bTask=new CannonFall();
        bTask.setUP(canvas);
        Timer bTimer=new Timer();
        bTimer.schedule(bTask, 1000, 2000);
        
        fireB.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                cTimer.cancel();
                bTimer.cancel();
            }
        });

        
        canvas.getChildren().add(center);
        canvas.getChildren().add(ground);
        canvas.getChildren().add(fireB);
        root.getChildren().add(canvas);
        
        Scene scene = new Scene(root, SCENE_WIDTH, SCENE_HIGHT);
        
        primaryStage.setTitle("Animation Testing Screen");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

The code works if I comment out the line with the Media class, but I would like to add music in the background. I know that I have to then use the MediaPlayer class after this and pass the Media object to it followed by a call to the play() method, but for some reason it produces the following errors when it is not commented out:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class NewFXMain
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    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:184)
    ... 1 more
Caused by: java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.NativeMediaManager (in unnamed module @0x7e50493c) cannot access class com.sun.glass.utils.NativeLibLoader (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.utils to unnamed module @0x7e50493c
    at com.sun.media.jfxmediaimpl.NativeMediaManager.lambda$new$0(NativeMediaManager.java:111)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
    at com.sun.media.jfxmediaimpl.NativeMediaManager.<init>(NativeMediaManager.java:108)
    at com.sun.media.jfxmediaimpl.NativeMediaManager$NativeMediaManagerInitializer.<clinit>(NativeMediaManager.java:78)
    at com.sun.media.jfxmediaimpl.NativeMediaManager.getDefaultInstance(NativeMediaManager.java:90)
    at com.sun.media.jfxmedia.MediaManager.canPlayProtocol(MediaManager.java:78)
    at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:240)
    at javafx.scene.media.Media.<init>(Media.java:393)
    at NewFXMain.<init>(NewFXMain.java:60)
    ... 14 more
Exception running application NewFXMain
C:\Users\User\AppData\Local\NetBeans\Cache\12.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\User\AppData\Local\NetBeans\Cache\12.0\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 1
    Unrelated to your question (which did not need to include the animation code for a minimal example anyway), but I recommend you use [JavaFX animation facilities](https://docs.oracle.com/javase/8/javafx/visual-effects-tutorial/index.html) (e.g. timeline, transitions or the [animation timer](https://gist.github.com/james-d/8327842)) to handle animations instead of the way you are currently doing it. – jewelsea Nov 18 '21 at 23:12
  • Yes, I forgot to include the code for the other animation classes that I created but those other timers(CTimer and PTimer) are classes that extend the AnimationTimer class. I don’t think they affect the media problem as the work properly and create an animation. – NewProgramer Nov 20 '21 at 16:13

1 Answers1

3

This is a java platform module access issue as can be seen from the error message:

because module javafx.graphics does not export com.sun.glass.utils to unnamed module. 

Study:

Ensure that you have followed the getting started instructions for JavaFX in NetBeans from openjfx.io to the letter.

Also, understand that javafx.media is a separate module, so you need to include it explicitly wherever you are adding or requiring other JavaFX modules.

For example, if your app is non-modular (does not define a module-info.java) and running on Windows, which would appear to be the case from your question, then you need to define appropriate VM options as outlined in section "5. Add VM options" of the linked document:

--module-path "\path\to\javafx-sdk-17\lib" --add-modules javafx.controls,javafx.fxml,javafx.media

Note, I appended ,javafx.media to the --add-modules argument, so that the media system will be loaded as a module, rather than loaded from the class path.

Running some JavaFX framework code from the class path rather than the module path (as occurs when the needed modules are not added or required), is not a supported execution mode and is what has caused your error.

jewelsea
  • 150,031
  • 14
  • 366
  • 406