Music in .jar and BasicPlayer 3.0 - java

Im working with Netbeans. Im using BasicPlayer 3.0 libs, to play a song, it plays fine when a run project with Netbeans BUT once a have my .jar file (with my lib folder and basicplayer3.0 jars) when a run my .jar project, mp3 song NEVER plays.
Any help?
Thanks!

In all likelihood, your application is not finding the mp3 file/resource from within the jar. If your mp3 file is held inside of the jar file, then you cannot access it as a file, but rather as a resource, and the path to the resource is different from that of a file. For a resource, the path will be relative to your class files.
For more help, show us the code where you obtain and try to use your mp3 file.

Related

Playing MP3 files from a jar

I'm trying to prank my friends with the semester's final Java program. It's a version of Space Invaders, and I added some annoying mp3 sounds to play after the game closes. I used Javazoom to play the mp3, and everything worked before exporting the project.
I learned resource files needed to be kept in a resources folder, which I have made. mp3 files and png files were placed in the same folder, and using code like the following works fine to display the images.
Image bg = Toolkit.getDefaultToolkit().getImage(getClass().getResource("./resources/bg2.png"));
However, the mp3 player cannot seem to access the resources folder. The InputStream always comes back as null when using this code:
//where fileName = /C:/Users/myName/JavaF16/Personal/bin/SpaceInvaders/resources/6.mp3
InputStream is = SpaceInvaders.class.getResourceAsStream(fileName.toString());
Player playMP3 = new Player(is);
fileName is a valid location, I can paste it in my file explorer and the mp3 will play. I read in that it needed a / on the front, but using that had no effect. I will admit that I'm not sure the project's folders are arranged properly, our class never taught about build paths or even source folders. But I did add all the resources to the build path. Here's a snapshot of the folder arrangement.
If I look at the exported jar in winRAR, all of the resources are included.
Any help would be much appreciated, thank you.

How can JavaFX Media read .mp3 files inside runnable .jar file?

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?
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.
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....!

Images and text file doesnt show in the exported runnable jar file

I am working in Java Swing application. I am referring image in my program and it work fine when I run it in Eclipse but when I run it after exporting it doesnt show those files.
My directory hierarchy:
Code 1:
Code 2:
While exporting the jar I have selected
Library handling: Package required libraries into generated jar.
after converting jar to zip, the zip file contains all the resources folders in it.
and also classpath file contains resources folder included.
I have referred many answers here but nothing helped me.
Reffered Questions :
Exported JAR Won't Read Image
Picture inside .jar file wont work when I export it
Exporting Images with JAR in Eclipse (Java)
Eclipse exported Runnable JAR not showing images
Java images not appearing in JAR file
Eclipse exported Runnable JAR not showing images
Try new ImageIcon("/res/images/icon.png") or new ImageIcon("/res/images/icon.png") depending on where you can find the image in the jar file. Because I think without the starting "/" it will try to find it outside the JAR file.
If this is not working then you can try loading the file with [yourclassname].class.getClass().getResourceAsStream(...)

How to export a a jar file correctly?

I mean I tried to export my java game like this : EXPORT>Jar File but if I do this it doesn't start.
And if I export to executable jar file it doesn't export my resources into the jar file.
I mean if I play the game in eclipse the sound works. But if I export to executable jar file it doesn't work. I guess it is not exporting the sound too.
This is the code I tried to use to launch the jar file :
java jar -cvfe ProjectZero.jar Main.Launcher Main.Launcher.class
Here are two solutions for solving this problem, hope this is clear enough:
Solution #1 - You want your resources outside of the JAR file
Just copy/paste the folder containing your resources in the same folder containing the JAR file. (Make sure the directory matches pathes mentioned in the application.)
Solution #2 - You want your resources inside the JAR file
If you want the resources to be directly included in the JAR file, you could use the function getResource() to get the images/sounds. Then make sure that resources are visible in both: "/src" and "/bin" folders.
For example, if you have the following application code:
ImageIcon myIcon = new ImageIcon(this.getClass().getResource("/resources/icon.gif"));
your file should be visible in:
/myApp/src/resources/icon.gif
/myApp/bin/resources/icon.gif
Then you can export your application as a JAR file, it will contain the resources.

How do I get a .wav or a .mid file to play outside of netbeans?

I have created a simply login screen for a test game and it plays a .mid music tune. The .wav sounds are played when you hover over a button. When I Build+Compile the program, the both sounds play inside netbeans. When i run the .jar file outside of netbeans, it does not work. Any suggestions...
P.S. The sounds are in a folder, inside the src folder, called resources. For .mid music, I use the sequence, and for .wav I use AudioInputStream and such.
It is most likely that you are attempting to access application resources as though they were a File. An application resource would usually be inside a Jar file, and must be accessed by URL. To form the URL, use something like:
URL urlToMid = this.getClass().getResource("the.mid");
If that is not the case, then the next most likely problem is that the resource is not being included in the Jar.
Without a code example, it's hard to help you out. Do you get an exception?
My guess is that your application cannot find the music files when running outside of NetBeans. Your home directory must be set to some value while running from NetBeans (probably pointing to your src/resource folder), but you specify a different home location (or none at all) when running outside NetBeans.
If the files are included in the generated .jar file, another possible problem is case-sensitivity. If the file is load from the regular filesystem (e.g. from within NetBeans), it depends on your operating system's filesystem if the filename is case-sensitive or not. On Windows it is not.
Once the file is loaded through a classloader (e.g. using getResource()), the filename is case sensitive and Alert.wav is a different file than alert.wav.
Make sure the filename in the source code is exactly the same way as it appears in the filesystem.

Categories

Resources