It's a bit confusing with URL and URI. In my project there is a package "sounds" with a .wav file in it. I want to play it via JavaFX MediaPlayer which takes a Media instance. The constructor of Media needs an URI. First I wrote
Media audio = new Media(new File("sounds/my audio.wav").toURI().toString());
But it only works while starting the program through the IDE. When I export my project as an executable JAR it doesn't work anymore, because it is trying to load the file from the direction where the JAR is lying. It should load it from the package instead which was exported into the JAR. I want it to load from the package so I need ClassLoader but it creates an URL. I could write
Media audio = null;
try
{
audio = new Media(ClassLoader.getSystemResource("sounds/my audio.wav").toURI().toString());
}
catch (URISyntaxException ex)
{
ex.printStackTrace();
}
That's neither beautiful nor convenient. Unfortunately it has this checked exception... And I would like to create the Media variable as a constant in an interface, where I can't even use try-catch.
So how do I have to write it? Just want to get my file in one line.