Java image loading doesnt show up in jar - java

I created a small image loading application, it just loads a .png inside the res folder. In eclipse running it will just work fine. But when I export it to an jar the image isn't included in the jar, so it can't load it.
I tried to add the res folder to the source path and it will include the image, but it includes it in the root off the folder (Which isn't where the image is supposed to be in)
Does anyone have an idea how I can add the res folder in the jar?
It does load the image when I manually (With Lubuntus archivemanager) put the res folder in the jar.
Any ideas how I can add it in the jar with eclipse?

You are on the right track when trying to make your "res" folder a java source folder. If you'd like to preserve "res" in the final path of the resources in the JAR, the res folder must sit inside a folder that's designated as the source folder.
You can either move res folder under your src folder or create a parent folder to hold res...
Option 1:
src (source folder)
.. res
.. .. image.png
Option 2:
src (source folder)
resources (source folder)
.. res
.. .. image.png

If you chose the "Export -> JAR File" option, you can select all folders and files you want to include. Those will be included properly, i.e. with their full name.
If you chose the "Export -> Runnable JAR file" option, this is not possible. In that case you can save the export script as an ANT build file. In the build file you can edit the export target to include the res folder and its contents.
That being said, in the long run, for making releases/JARs, Eclipses built-in functionality is pathetic. You really want to go the ant/maven way. It takes a bit of reading, but after that it's all automatic and far less hassle and far more flexible than anything Eclipse offers.

Related

How to include some folder in target but not in out folder in IntelliJ IDEA

Surely there will be an answer to my question but I've been trying for days and I can't in any way.
This in the picture is the structure of my target folder. I would like to make the folders: image and video not be placed in the .jar file once the file is built but be placed as external folders in the same location as the .jar file.
However, I would like the folders to remain in the target file to allow me to access them via class.getResource during the test phase but once I have obtained the .jar file I have to be able to change these images at will and I don't want them to be present inside the .jar.
I hope I made myself clear.
I tried something inside the project structure but with no result
Structure folder

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

Eclipse export JAR and maintain directories

I have a resource folder where I keep images for my project.
So my structure ends up being
Project
-src
-bin
-res
Res is the folder that contains my images, and in my code, images are referenced like so.
"res/image.jpg"
That works fine when I run my project from within Eclipse, however if I wish to export it to a runnable JAR file, it puts all of the images into the root folder within the JAR. Is there any way to make it put the images in a res folder within the JAR?

Adding folders to jars

I created a folder called "res" in which I put all the sound files and picture files that my application uses. I added the folder to my project as a library so using
"/filename.whatever"
as the filepath works when I run the project in netbeans.
However, when I try to clean and build it says:
Not copying library path\res , it's a directory.
How do I get netbeans to put the folder in the jar so that it still works with the same filepath?
This is the answer of the SAM
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("**resources/images/logo.png**")));
and:
if (common.readFile(getClass().getResourceAsStream **("/resources/allwise.ini**"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
Src : How to correctly get image from 'Resources' folder in NetBeans

Trouble making executable jar

When I try to make an executable jar in netbeans I get this error
C:\lwjgl\lwjgl-2.8.5\res is a directory or can't be read. Not copying the libraries. Not copying the libraries.The res folder holds files like jpgs and wavs that the program relies on to function. I'm using lwjgl, would that be part of the problem? What could be causing this?
try this
Right click on your project folder and click on >>>>clean and
build<<<<
right click on your project folder click on >>>Properties<<<< you
should see the location of your project folder normally this should
be in your documents folder under NetbeansProject folder. locate
your project name folder inside is a folder called >>>dist<<< in
there you should find your project name with a small java cafe
image which is your jar file.

Categories