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)