Audio doesn't play in JAR File [duplicate] - java

This question already has answers here:
Audio does not play in Jar but does in eclipse
(4 answers)
Closed 7 years ago.
I've got a problem regarding audio.
I am coding in Java using Netbeans, and I want to export my project so that it doesn't require Netbeans to run, so I decided to create a JAR File. I put all the images, and audio files (aiff) for my program in the src folder so that they could be accessible from outside the coding console.
I have declared my InputStream and AudioStream as global variables, and I am using them within a try catch block, where the code is run when a button is clicked.
AudioPlayer.player.stop(audios);
try{
in = new FileInputStream (new File("src/glory.aiff"));
System.out.println("Button Clicked");
audios = new AudioStream (in);
AudioPlayer.player.start(audios);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
This code perfectly fine when I run the program on Netbeans, however when I create a JAR file, the audio doesn't play, and an error pops up saying that the file cannot be found. I've tried this on my computer, and other computers, and the same problem occurs.
What I find strange, is that the pictures I mentioned earlier are in the same SRC folder, and they display correctly. I have linked these pictures through the use of icons, and Netbeans auto-generates code for that.
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG_2138.JPG")));
I have done research of my own, however other people who have this problem don't seem to be using the same AudioStream as myself and I don't know how to adapt it to solve my problem! I've also tried changing the file path of the audio, but whenever it has worked in Netbeans, it has not worked in my JAR file.
Would anyone please be able to tell me how I could fix this problem?
Thanks,
Rohan

I think it would be easier for you to create an setup for your java program in Netbeans and it will still be able to run without Netbeans.
You can do so using a software called excelsior jet.
All you would need to do is watch a short 5 min video on youtube on how to use this and you will have your setup which you can install in your computer and other computers.
Also before you use the software to make the setup it would be good for you to check whether your program actually runs properly as a jar file.
To do so just go to where you save your Netbeans projects, click the project for your program and then enter the "dist" folder(This is where the jar file for project is stored). Click the jar file to check whether the project runs normally.
If you do not find your jar file in the dist folder then its most probably because you tampered with your "manifest.mf" file or your "build.xml" file in your java project.

Related

How to add media resources properly into a (Java) Gradle artifact by using IntelliJ IDE?

I am a beginner, so I'm sorry if the question is too basic.
I've just built a little GUI based program that repeats a certain sound (a short alarm sound right now) continuously after a given amount of seconds, resulting in a sound cycle.
The issue is that when I run the program inside the IDE, it runs perfectly. But, when I build it, the built artifact (the *.jar file) does not work well. I mean, everything works well, but it plays no sound.
I've been searching for answers in the community and I've found some instructions about how to build the artifact properly. At the moment, they were based on a Maven setup most of the times. Either way, I used those answers, but the *.jar file I'm getting has NO resources inside. So, neither the required sound file.
I've tried to put the *.wav file manually, using the project artifact options, but this didn't work either, even if I am positively getting the sound file in the built *.jar file artifact.
I've also tried to convert the program to a console one (non GUI) to see any possible exceptions, but then my program didn't play any sound at all. Not even running it from the IDE. So, it's got even worse, and this is, in fact, a different issue that I don't want to mix in here, and I hope I am not wrong on that point.
In most of the answers I've found, they were giving a lot of importance on how the resource was "fetched" inside the code. In fact, it has taught me a better way to do so. I was using quite a weird way to get the absolute path of the file... But then I used:
MyClass.class.getResource("sound.wav");
And now I'm using (with the same result):
ClassLoader.getSystemResource("sound.wav");
Both "resource getting" options had also been tried with a slash bar before the file name ("/sound.wav"), with, again, the same unproductive result as I've already exposed.
Just to make it totally clear, as I mentioned in the question title, I am working with a Gradle project in the IntelliJ IDE.
Thank you in advance for your help.
The code you are using to load the resource as stream from the classpath is not correct.
You first get the URL of the resource with URL res = ClassLoader.getSystemResource("sound.wav");, then use getPath() method to convert it to String.
It works when the resource is not in the jar. But when it's in the jar, file URL becomes invalid and the code fails with FileNotFoundException.
Instead of that you need to change the code in MediaPlayer#playSound, replace
final AudioInputStream in = AudioSystem.getAudioInputStream(fileUrl);
with the following as a quick fix:
InputStream is = getClass().getResourceAsStream("/sound.wav");
final AudioInputStream in = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
This will load the resource stream directly from the jar and the file will play.
See this answer for more details.
Your app will not run with Gradle because there is no support for GUI Designer in IntelliJ IDEA Gradle builds yet.
To build the jar I had to create an artifact manually and include the sound file there.

Mac OS El Capitan never opens .jar files

Every time I try to save a.jar file for my programming course and it doesn't open, and yes I save the META.MF whit the correct path, I even stop making them on the Terminal and use Net Beans but same problem Error: Could not find or load main class a3t8e1.A3T8e1 maybe I need to download something else.
I search on the internet and download the jar launcher but nothing better.
this is the .jar unpacked
Please try to right click the .jar file and click the Get Info. Under the Open with, select the Jar Launcher.app. After that, try to reopen your related .jar file.

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

Loading Images from Jar file only works on Development Machine?

I am having trouble loading Images contained within a JAR file. I have read a number of other posts related to this. But cannot find the answer. Now here is where it gets complicated.. This all works fine if I am using a Runnable JAR file exported from Eclipse and Run it using the standard JRE. However This JAR file is actually a plugin for a piece of software called pro/ENGINEER which has it's own JRE that is used to run the JAR file. The strange thing is.. that this works fine on the development machine, that has eclipse installed and so on, but doesn't work on any of the client machines?! But I can't see what is different.
I am trying to load the image using:
ImageIcon icon = new ImageIcon(this.getClass().getResource("resources/Header.png");
This method is called from my 'Start' class, and the Jar is structured as follows:
load
Start.java
load.resources
Header.png
If I Open the Jar file with WinRar you can see the Image definitely exists in the jar, in this position.
I know this may be a very specific question but if anyone can be of any help that would be great.
I did not have the particular problem you're having now, however I ran into the problem of not finding a resource in a JAR file several times in the past.
The solution was to not get the resource from the class, but from the class loader as demonstrated below:
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("resources/Header.png"));
Mind the .getClassLoader() call after the getClass().
Hope this helps.

Cannot read from an runnable Jar file

I'm trying to make a mp3 player and I used javazoom libraries. I save mp3 path in library.txt file to reopen them. There is not any problem with openning library.txt which is in the jar file.
Normally with eclipse program It is working well but when i created a jar file the problems begins.
I can't use absolute paths to open any mp3 file with my jar file.
File file = new File("/Users/orcungumus/Music/iTunes/iTunes Media/Music/Bruno Mars/Unorthodox Jukebox/1-16 16 Locked Out of Heaven.mp3");
try {
player.open(file);
} catch (BasicPlayerException e1) {
JOptionPane.showMessageDialog(null, e1.toString(),
"Error", JOptionPane.INFORMATION_MESSAGE);
}
For making thinks easier to understand i used directly path of an file. It works for eclipse, but with jar file it can't open file.
This is the error which i take:
If it is important i use mac os.
Edit: I realized that this is not about absolute path by making mp3 path relative. Libraries give an error about audio format. So, the problem is still exist. what can be the differencies between runnable jar file version and eclipse run for an project.
well, that's hard to find out.
now your problem is unable to open a audio file, which I can't handle.
However you said that the programme run perfectly using eclipse, so the question became
the different between running in eclipse and normal jar file.
FIRST, eclipse not directly use java.exe nor javaw.exe for execution. Instead, eclipse use some custom defined library in order to redirect the console, handling exception, etc.
(That is nothing to your problem)
SECOND, eclipse will set the working directory for you automatically (to the project root directory), where may contain some necessary native library.
THIRD, eclipse also define some path for native location for library, I think you may use eclipse built-in export to create the jar file which however not include the native library.
the second and third difference may help you, because they are quite effective to jar file.
I have just browsed javazoom briefly, I don't know whether there is native library. If not,
I sorry that you have to find other solution.

Categories

Resources