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.
Related
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
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....!
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.
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.
I've been working on a little project that requires external images for display. I'm not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project so I can share it with others. Right now, it seems the only way I can get my images to show up is if I assign a specific folder on my hard drive and have the image paths in the code go to that.
I'm looking for a way to export the images as part of my JAR or as part of the same package so when I go to send this program to other people, I don't have to send them a separate archived folder of images. I'd also be interested in learning what I need to do to have my code reference the images within that package so they'll work without an external folder.
I have read about some kind of package system within Eclipse, but have thus far had no luck in figuring out how to use it. Could use some explicating!
Thanks in advance to anyone willing to give me their two cents.
Something I would have found useful with this answer is the following: make sure you put your images/files in the same eclipse folder (or sub-folder below) as your source code. I created a folder "images_ignored" using eclipse, added it to the build path but still it refused to be included in my JAR file (when creating an executable JAR).
Just drag the images folder into your Eclipse project, then choose to "Copy New Folder" or "Copy File and Folder" depending on Eclipse version, and then right click on the image folder (in Eclipse) and --> build path "use as source folder".
you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)
Use getResource() to load the images:
ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
If you're using JDK 1.7 or JDK 1.8, you might want to use the NIO.2 API.
for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
if ("jar".equals(provider.getScheme()))
return provider.newFileSystem((new File(Start.class
.getProtectionDomain().getCodeSource().getLocation().toURI()))
.toPath(), new HashMap<String, Object>());
}
If you enter this code into a method that returns a java.nio.file.FileSystem, you can call the method to get the FileSystem for the JAR file.
To get the path to access the files inside your JAR file, you can use the following method, which then allows you to read the files however you may want.
fileSystem.getPath("/images/image.gif")
If you would like to be able to run this in Eclipse, make sure you surround the call to the method with a try/catch IOException and assign to your FileSystem object the following.
new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.toPath().toString();
This will allow you to run your program whether it's compressed into a JAR file or not.
I recommend you get used to using NIO.2, since it is a very powerful API.
If you add a folder to build path you can retrieve the images either in eclipse and when you exported it in jar file, just remember to don't reference the image with the path like img/myImage.gif but only myImage.gif !