Including Images with an executable jar - java

I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet
I am packaging a quick game I made into a executable jar but I didnt reference the images correctly I just referenced the files
background = ImageIO.read(new File("wood.jpeg"));
I have my classes in src default package
Im not sure where I should add the images or if I have to add it to the build path
or correct way of adding the images to the build path in the newest version of eclipse

Files in a Jar are not files in the sense of a file on disk. They are simply a (possibly) compressed stream of bytes.
Java makes it easy to extract these "resources" from Jar files through the use of the ClassLoader
background = ImageIO.read(getClass().getResource("/wood.jpeg"));
Should work...
This will return a URL which ImageIO can use to load the resource.
You could also have a read of
Classpath resource within jar
Jar get image as resource
Load a resource in Jar
And I could list some more. So, yeah, it gets asked a lot ;)

Try using Constructing Runnable Jar using the Eclipse.
R_Click on the Project in the Package Explorer --->
Export ---> Runnable JAR file --->
Select the option of Package required libraries into generated JAR

Related

Where is the right way that should you put your resources (i.e Images and Sound files) when trying to create a JAR file?

I am using Intellij IDEA and whenever I build a JAR artifact it places itself on the productions folder. When I try to run the JAR file some custom defined functions (i.e. setting Icons on Components and adding sounds) are missing, while running it on the Intellij's IDEA is functioning correctly. I know that it involves using getResourceAsStream method but is there some sort of right way to place external resources?
Additional questions I may add, where is the correct destination for placing the META-INF folder? Should I make a resource folder for all my external files?
This is for Eclipse users, I believe the same thing can be accomplished in other IDE's relatively easily.
Create a resources folder
Put your non-code files in it
Use as "Source Folder"
After creating the JAR, the contents of "resources" will be packed in the JAR.

Accessing files with relative filepath inside built jar java

I have a project which uses relative file paths to access resources. While the project is run in IDE, the path looks like.
\\src\\musicTest\\Track4O.mp3
When the project is built into a jar file, this no longer works. Upon opening the jar file, I noticed that the file structure looked like
musicTest.jar\\musicTest\\Track4O.mp3
In both cases, the class attempting to load the resource was in the same location as the mp3.
If anyone has a solution for either: maintaining the file path so that the build will run as intended or: solving the relative file path problem, it would be much appreciated.

How to include resource folder in executable .jar file in eclipse?

I need to create an application for sorting various types of polygons using various parameters such as height, volume or base area. Arguments for Filename which has parameters for polygons, Sort type, Sort method will be pass through command line.That file is in my resource folder outside my src folder in a project. I have implemented all programs, It works fine when I run using pass arguments through eclipse run configuration. But when I try to run my .jar file using cmd same arguments it gives me FileNotFoundException.
I opened my jar file using 7zip and noticed it never extracted my resource folder in .jar file. I searched online and tried including my resource folder in to build path of eclipse. But still does't work.
Follow these steps:
1) click project -> properties -> Build Path -> Source -> Add Folder and select resources folder.
2) create your JAR!
EDIT: you can make sure your JAR contains folder by inspecting it using 7zip.
Reefer this link as well How do I add a resources folder to my Java project in Eclipse
First, you need to create a source folder for resources, for instance name it res, and then move your image folder to res. When you generate the jar file, you will see the image folder, not the res folder or files separately.
This comes down to how you are generating the JAR file.
When you're exporting the jar in eclipse make sure to checkbox the button that says "Export java sources and resources" https://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/ensureJavaFiles.jpg
There are a lot of ways to do this one is to use Gradle is the recommended way, something like this will work Creating runnable JAR with Gradle

Export to JAR with external JavaPOS Files

I've currently finished my project, but can't get it to work when it is exported. I use JAXB to read and write XML Files and also have dependencies on other external Folders, which are needed to use a POS-Printer.
I've managed to link my external XML Save-Files with absolute paths, but not with relative paths. So that worked, although not the way i wanted. Yet, using the external class folder for the printer didn't work at all.
This means, that in my Eclipse Project Build Path i've added a class folder, which contains all of these needed files (which are not only jars, so adding them one by one wouldnt work). So exporting my project to a jar either includes all the files into the jar itself, or doesnt include them at all.
Everything works perfectly in Eclipse, but not when i export it.
My folder structure looks like this:
src
/model
/view
/control
data
/articles.xml
/...
JavaPOS <--- needed folder with all its files
/jpos.xml
/xerxers.jar
/swt-..-.dll
I've tried:
InputStreams is = getClass().getResourceAsStream(url);
absolute paths
manipulating the manifest file and/or jar structure
runnable and non runnable jars with nearly every combination of options
putting the files inside the library "by hand"
changing the build path of the project
My Question is:
How do i get my jar-file to know where these files are?
EDIT:
Do you think Maven or an Ant file could solve my problems? I don't have any experience with those.
The Problem was, that i had more than one JRE installed and that the one eclipse was using, had all the dll files, but the other ones didnt have it. So i had to add them manually, because reinstalling the drivers of the printer didnt change anything. Gotta fix that somehow, but right now it works and that is all i wanted.
Turns out i didn't even need that Folder, just needed one file out of it and the missing dlls.

How to package a jar file along with reference files?

Using eclipse, exporting a runnable jar file is pretty simple when I'm only using the application on my computer. Any files that the program is using (sprite sheets, audio tracks, etc.) only exist on my computer, so sending solely the jar to another machine won't work.
What is the easiest way to package a jar along with all the necessary files so that I could run the program on any machine?
I see from your tags that you are working in Eclipse. I am not sure if this method will work in other IDEs and I don't think it'll work at all if you're doing everything manually (it relies on the compiler automatically copying resources over to the bin folder.
The simplest way (at least what I use) is to define another sourcefolder (I like res).
Then you can just add packages to this source folder and dump the relevant images. Then rebuild your project.
Finally, you can use getClass().getResourceAsStream("package/path/file_name.whatever"); to get your files.
After an export as jar, it should work, even on other machines.
If you don't require the other files to be actual files on the file system (which means you can't use File, FileInputStream, etc) then you can use the resource system. If you put them inside the JAR, you can access them like this:
InputStream fileStream = SomeClassInYourJarFile.class
.getResourceAsStream("/path/to/file.png");
This example would give you an InputStream reading from the /path/to/file.png entry in your JAR file - that is, the "file.png" file inside a folder "to" inside a folder "path".
This does not require the files to be in a JAR file - it can load them from wherever your .class files are stored, JAR or not. If you put them in your source folder, Eclipse will automatically copy them to that place - so the above line would also work if you had a package path.to containing a file called file.png.

Categories