This is not a duplicate, because Media objects cannot be initialized with getResourceAsStream(). From the docs: "only HTTP, FILE, and JAR URIs are supported."
My JavaFX project works perfectly when I run it from Eclipse, but breaks when I run the executable JAR that I export. I have tried each type of library handling, but none of it works. I'm using JavaFX 2.2 and Eclipse Oxygen March 2018.
public static Media gameMusic;
public static MediaPlayer gameMusicPlayer;
public static MediaView gameMusicMediaView;
// adds music, once for each audio file to be played
gameMusic = new Media(new File("resources/data/music/Kevin_MacLeod_-_Ouroboros_-_Full_Mix.wav").toURI().toString());
bulletSound = new AudioClip(new File("resources/data/music/Photon gun shot.wav").toURI().toString());
explosionSound = new AudioClip(new File("resources/data/music/Explosion+3.wav").toURI().toString());
The directory structure:
+src
-source files
+resources
+data
+music
-music files
+fxml
-fxml files
+other folders
The error output:
MediaException: MEDIA_UNAVAILABLE : /Users/username/Desktop/resources/data/music/Kevin_MacLeod_-_Ouroboros_-_Full_Mix.wav (No such file or directory)
at javafx.scene.media.Media.<init>(Media.java:407)
at application.Main.start(Main.java:77)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Note: when I fixed the code for the filepaths to include the executable's name, it breaks in both Eclipse and the executable