Eclipse export JAR and maintain directories - java

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?

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

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.

Java image loading doesnt show up in jar

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.

Java JAR Export Images Folder Is Wrong

and thank you for reading this and any help you give me. Well...I'm using Eclipse, and I'm trying to export my project as a .jar.
The problem is...I have a folder that is listed in Eclipse as a class folder in my build path that I named "res." Inside the res folder is another named "Images." When I export my .JAR, and look at the files in it, all I see is the Images folder, not the res, which messes up my image loading methods in the project.
Are there any ideas on either how to get the res folder to be exported differently or how I could make my image methods to work properly both in test mode in Eclipse, and as a .jar?
Your images should be in a source folder (let's call it "resources"), and respect the same package hierarchy rules as Java source files.
If you load your images with SomeClass.class.getResourceAsStream("/res/images/foo.png"), then there must be a package named "res" in the resources source folder, containing a package named "images", containing foo.png:
src <- source folder
com
foo
bar
Application.java
SomeClass.java
resources <- source folder
res
images
foo.png
The generated jar file will look like this:
theJar.jar
com
foo
bar
Application.class
SomeClass.class
res
images
foo.png

Categories