1

I have trouble getting my Java application with a JavaFX MediaPlayer to read the .mp3 file that is embedded in the runnable .jar output file.

I have tried every single solution from Google, e.g. toURL, toURI. Some of the solutions worked (the music was read and played) when I ran the program in Eclipse JVM but all of them failed when the code and the music were packed into a runnable .jar.

The .mp3 file I am going to read is located:

In Eclipse: Java Project --> src --> img --> music.mp3

In File Explorer: C:\Users\Me\Desktop\Java Project\src\img\music.mp3.

  • The following code worked in Eclipse JVM but failed when I executed the .jar.

    media_player = new MediaPlayer(new Media(
    Game.class.getClassLoader().getResource("img/music.mp3").toString()));
    
  • System.out.println() of the above string gave me the following string in the Eclipse console:

    file:/C:/Users/Me/Desktop/Java%20Project/bin/img/music.mp3
    

As I need to distribute the runnable .jar, I do not want to separate the .mp3 file from the .jar because it may cause inconvenience to the users.

Please tell me if the JavaFX Media or MediaPlayer are not supposed to handle this situation.

EDIT:

After reading the comments, it seemed that I had to choose Extract required libraries into generated JAR instead of Package required libraries into generated JAR when exporting the runnable .jar.

However, this raised another problem. ProGuard, the Java obfuscator I was using, could not process the libraries and prompted me duplicate definition of library class errors. I was forced to choose the third option, Copy required libraries into a sub-folder next to the generated JAR which allowed the music to be played but it resulted in all external libraries being separated from the .jar.

Is there another way of getting the obfuscator to work, external libraries to be embedded into the .jar and the music to be played?

blackr1234
  • 1,420
  • 12
  • 23
  • Possible duplicate of http://stackoverflow.com/q/15660824/2670892 The key is to use `toURI` method of `getResource`. – greg-449 Aug 06 '15 at 09:31
  • @greg-449: I don't see any difference in output between `getResource().toURI(...).toString()` and `getResource(...).toString()` – wero Aug 06 '15 at 09:39
  • There won't be when the mp3 is a separate file, I believe there will be when the mp3 is part of a jar. – greg-449 Aug 06 '15 at 09:41
  • can you also show the value of `Game.class.getClassLoader().getResource("img/music.mp3").toString()` in the .jar version? What error do you get, an Exception? – wero Aug 06 '15 at 09:41
  • @greg-449: I ran an example for a jar URL and there is no difference. – wero Aug 06 '15 at 09:43
  • Thanks for your replies. I get the same println ``rsrc:img/music.mp3``, regardless of ``toURI()`` when I execute the .jar from PowerShell. @wero, the error message is ``java.lang.UnsupportedOperationException: Unsupported protocol "rsrc"``. – blackr1234 Aug 06 '15 at 09:56
  • 1
    can you try the solution of https://stackoverflow.com/questions/7507156/java-net-malformedurlexception-unknown-protocol-rsrc – wero Aug 06 '15 at 09:58
  • @wero Thanks, using ``Extract required libraries into generated JAR`` (instead of ``Package required libraries into generated JAR``) worked. And I have figured out that I actually need to choose the third option ``Copy required libraries into a sub-folder next to the generated JAR`` in order to get ProGuard (the Java obfuscator I am using) to work (error messages of ``duplicate definition of library class`` are prompted when I choose the ``Extract ...`` option). – blackr1234 Aug 06 '15 at 10:15

2 Answers2

0

Although I have not managed to solve this problem, I tried another approach to play .mp3 files inside the JAR and use ProGuard to obfuscate the code.

The solution was to use an external library, JLayer Player by JavaZOOM, instead of JavaFX.

blackr1234
  • 1,420
  • 12
  • 23
0

When you create an executable jar file, a library file is also created in the same drive where the jar file is. However the important point to note is that you have to physically place the media file say e.g. song.mp3 in the directory and the jar file will execute....!