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

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

Related

Storing data in text files with Java/Maven

I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader

How to edit .java files imported in Eclipse from a .jar file

I have a question, perhaps it was already answered, but i didn't manage to find it and I appologize if the solution already exists (let me know if it is before deleting my thread).
Problem is:
I have created a program on another PC and exported it from eclipse as a .jar file. It works on my main PC when I double click on it but when I import it in Eclipse I can't find the .java file. So i can't edit it.
What I have done so far:
In eclipse I have created a new empty project
I have right clicked,import, archive file, selected the .class files that eclipse sees, but when I am in the Project Explorer in Eclipse I can't find the .java file where the main is. I mean I can click run as a program and it works, but there is no .java file, only .class files. What am I doing wrong?
That cranes.class should be cranes.java. At least on my other PC it is.
Program works fine, but I can't edit it on my main PC. What am I doing wrong?
Thanks and best regards
You need to select the Export Java source files and resources option while creating the jar file and then your Java files will be available on importing the project from the jar file.
This is similar to how you use other libraries. You depend on the Jar file which contains class bytecode (compiled) of java code. You can't edit any of such files directly in the project you are using it. Thought you can always extends functionalities in your current project using simple inheritance concepts.
If you think such functionalities are trivial you should prefer to change in the original project rebuild the jar and use the newer version of jar.
However if you feel similar things for 3rd party library you can
always make changes after taking fork from those library source
code (if open source) and build and use your own version or go
ahead and raise pull request if you are confident about your
changes.
Mostly when you build a jar file, all you have in it are .class files; these are the result of compiling .java files, and so are not editable with text editors.
You CAN create a jar file that contains .java (also known as source) files, and even a jar file that contains both .java and .class files, but if you ask eclipse to create a jar file, by default it is just going to put .class files and files from resource folders in it, not .java files.
Assuming from the question, the jar is a library created by OP, by compiling java files into class files and packing/exporting them. Although the class files can't be edited in any IDE, they can be de-compiled into Java files by using third-party applications.
I personally use IntelliJ for this de-compiling source files authored by me
Note: Although this gives OP the desired functionality, it may lead to violations if the classes are Copyrighted.
As IntelliJ states, they neither encourage nor discourage de-compiling class files and the decision is purely to the user's discretion.
EDIT: It is always recommended to use the original source files for editing. Try to host them on git so that it may be retrieved anytime required
It may be simpler to not use eclipse but jar/zip/tar your project directory on the one computer and simply extract it onto the other, then open that folder as a new project in Eclipse.
Best is the suggestion from #SanjayBharathi to use git and clone the repo on your other machine.

Exported jar finds some files (not all) even though the paths are the same [duplicate]

This question already has answers here:
I'm trying to export a runnable jar in Eclipse, but I need it to include some other files necessary for running my program
(3 answers)
Closed 8 years ago.
When I run my program from eclipse, it shows up fine, the images from Resources\ show up, as do the sounds from the same place, and the text files are found properly. However, when I export my jar, and copy the resources into it with 7Zip, the images will work, but the sounds and the text files can't be found, even though they're in the same folder, with the same path used to find them in my code. I can fix this by putting a folder next to the jar file named Resources, and put everything in there, but I'd like to know why just putting it in the jar file only worked for the images, and how I can get it to work with the text and audio files as well.
An example to show you what I mean:
File inventory = new File("Resources/inv.txt");
threadpath = "Resources/threads.wav";
enemy1 = new Sprite(new Texture("Resources/miniForestGolem.png"));
When I run it in eclipse, all three work fine, but when I export it, and put the resources folder in the jar file, only the image works.
Edit:
I know how to include my resources, and have done so, I'm asking about how/why some of the resources aren't able to be accessed, even after adding them in.
Ok, from your comments we can infer the difference between executing it from eclipse and executing it from a .jar.
From eclipse: it works, because all that new File(...) find an actual file in Resources/
From the .jar: it won't work, since there is no file in a relative ./Resources/ path from the execution path of the application.
The way to make it work is the next:
Make sure Eclipse recognizes Resources/ as a source folder (right-click on project properties, Java Build Path, and add it as a source path)
Look for a replacement for your API methods that, instead of File objects, use InputStreams. Once you have it, retrieve all your resources as InputStreams taken from the classpath. If you are inside MyClass.java, do this: MyClass.class.getClassLoader().getResourceAsStream("Resources/inv.txt"), etc.
What you have achieved by doing this: instead of File objects built on actual operating system files, you will be retrieving InputStreams read straight from your java application classpath. This way, you can package them into a jar, or into a WEB-INF/classes directory inside a web application, or a library folder in some application servers... wherever you like as long as it is into the application classpath. I would do this if I had to package your application in a portable and usable way.

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.

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.

Categories

Resources