Playing MP3 files from a jar - java

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.

Related

How deal with image paths in java?

First, let me clarify that I'm a Java programmer by hobby, English isn't my native language, and this is my first question in StackOverflow, so if I'm doing something wrong, I will be grateful in be advised.
When I need to use some image in my applications, I put the image file in bin folder of my project and use the following code to acces that image:
Image img1 = new ImageIcon(this.getClass().getResource("/confirm.png")).getImage();
Recently, I decided let user choose the image with a JFileChooser. The way that I made is get the image path and copy that image into my bin folder, so this way I can use the above line of code. Here's is how I'm doing that:
File outputFile = new File(System.getProperty("user.dir")+ "/bin/" + "default.png");
ImageIO.write(defaultImage, "png", outputFile);
That way I can get a copy of the file in my project folder (C:\Users\renan\Desktop\Image_Editor\bin\default.png).
Everything works fine when I run the application in Eclise, but the things start to go wrong when I create a Runnable JAR file (.jar). When I runed my .jar file over command prompt (java -jar "Image Editor.jar") I've got the following error:
java.io.FileNotFoundException: C:\Users\renan\Desktop\bin\default.png (The system can not find the path specified).
After think about this error, I realised that after create a .jar file I can't stay copying the images into my bin folder because now has just a .jar file, not separated folders like before.
My question is: What should I do to deal with images that will be used after the program had compiled?
Should I create a folder in my computer that will storage this application images and load the images from that folder? (like games have Screenshots folder to save their images catched by the user).
If anyone can advise me the right way to do this, I will very thankful.
Create a folder outside of the JAR. A JAR file essentially a *.zip file on Windows, therefore you can't change any of the folders in the JAR file after it's been compiled because it's in a compressed format.

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

Inserting icons in downloadable jar application

I am a novice programmer trying to create a jar file that people can download and demonstrates a...JRPG-ish upgrade system I have. As a little visual pop, I want the central button to display a little icon as an extra visual pop. While it works fine in Eclipse, when I export it to a JAR file the icon is no longer displaying (though the rest of the program works as intended.
I have managed to make Eclipse import the files I want into the JAR file, as seen below.
This is the code I currently have to display them in Eclipse
imgDin = new ImageIcon("icons\Mark_of_Din.png");
imgNayru = new ImageIcon("icons\Mark_of_Nayru.png");
imgFarore = new ImageIcon("icons\Mark_of_Farore.png");
How can I modify this code to display them inside the JAR file?
You should be using
new ImageIcon(getClass().getResource("/Mark_of_Din.png"));
That little package icon in the icons folder icon, means the files in the icons folder will be at the root of the classpath. getClass() gets the calling class, and getResource() locates a resource relative to the class. The / brings the search to the root.
You can extract the jar and you will see the icons folder is not there.
See Related Post
"While it works fine in Eclipse.."
That's because when you pass a String to the ImageIcon constructor, it is looking for a file on the file system, relative to the working directory. In Eclipse (and most IDEs), the default working directory is the project root, and that's where your icons folder is.

Music in .jar and BasicPlayer 3.0

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.

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