Java JAR Export Images Folder Is Wrong - java

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

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

Eclipse, Java from 2 src. folders?

Do anyone know what is wrong with this?:
-Launched Eclipse IDE(Java).
-Created new Java Project
-I got src folder (Source folder)
-Created a new Package in the src folder, and a java class in that Package
-And, I created a new source folder called Folder2
-And in Folder2 I created a new Package and Class file in it.
I tryed to connect these 2 classes from 2 different src folders, but the 2nd class is not found.
In the 1st class I used classTwo.main(null); , but the 2nd class from the 2nd source folder was not found.
I have tried same thing and its working for me.
See be tutorial Organizing sources
make sure setting is same as below:
When creating multiple source folders, and using the classes in them, you need to make sure that the source folders themselves are added to the Java Build Path settings in the project. To access these settings,
Right Click on Project Name in Package Explorer View -> Properties -> Build Path.
There you should see both of your folders shown as source folders.
This way, eclipse should take care of building the .java class files and generating the .class files into the build folder.
Another thing, I dont get why you are creating two different source folders for Java classes. The accepted and widely followed approach is to have multiple packages (even if there are hundreds of them) inside the src/main/java source folder for Java source files. The general project structure followed is:
src/main/java - Java Source packages with .java class files
src/main/resources - resource files like .properties, .xml files which are a part of the actual application code
src/test/java - Java Source packages for .java JUNIT tests.
src/test/resources - resource files needed for the execution of tests which are not part of the actual application running code. eg. specific setting files for JUNIT tests

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

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.

Categories