Reputation: 3
I have many sound and picture files I am using in a pokemon game I'm writing with Eclipse. Everything runs perfectly in Eclipse, but when I export it as a runnable jar, I get an error that the media is unavailable when I try running it.
This is the code I'm using for playing an mp3 file:
File file = new File("src/pokemon/mp3s/opening.mp3");
final String mediaLocation = file.toURI().toURL().toExternalForm();
Media media = new Media(mediaLocation);
mp = new MediaPlayer(media);
After exporting as a runnable jar, I execute it with java -jar Pokemon2.jar and get this error message:
Exception in thread "Main" MediaException: MEDIA_UNAVAILABLE : C:\Users\Nick\Desktop\src\pokemon\mp3s\opening.mp3 The system cannot find the path specified
How do I make my runnable jar be able to locate these additional resources?
Upvotes: 0
Views: 2237
Reputation: 489
Files in a jar are not accessible as regular files. Instead you have to access them with getResource
, which works when they are in the jar and out.
getClass().getResource("/pokemon/mp3s/opening.mp3");
Upvotes: 1
Reputation: 8652
Deployed jar file doesn't contains source files or src folder.
It is your IDE which make source files compiled and put them at other place, (e.g. in netbeans inside build\classes). And when you deploy your project it is your mp3 file will be present inside that jar file, not in src
folder.
You should try another approach.
suppose your file is present in src/pokemon/mp3s/opening.mp3
and a class is present in src/pokemon/Referer.java
Now you can access your mp3
file by taking Referer
class as reference :
URL fileUrl = Referer.class.getResource("mp3s/opening.mp3");
Upvotes: 1