Adding folders to jars - java

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

Related

Eclipse referencing folders inside a Jar file

In my Java project I have a folder like the one below, where I put inside some icons that I'm going to use with JTree library.
When I run the Jar outside Eclipse, I cannot see my icons.
If I open my Jar with archive utility, I cannot see the folder inside it. I also tried to add different folders and they are never referenced inside the final Jar.
So, is there a way to reference the folders in the Jar file?
You need to ensure that Eclipse knows that while building the Jar it has to include the newFolder inside the Jar. You can do that in Eclipse by following the below step in :
click project -> properties -> Build Path -> Source -> Add Folder and select the new folder you just created.
Now create you Jar and you will see your folder in the Jar.
Either move the folder (myFolder) into src directory or mark it as a source folder.

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?

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.

Eclipse: project directory isolated from source directory

I have Java code managed by a Git repository. Currently, the directory structure is
myProject |
-- src
-- bin
-- lib
-- .git
Currently, each developer creates its own development environment under his desired tool (Eclipse/Netbeans). I would like to create a Eclipse project for this code and add it to my source repository, so that new Eclipse developers don't need to create a new project and set the dev environment manually. I'd like to have this:
myProject
|
-- src
-- bin
-- lib
-- .git
-- eclipse
|
-- .settings/
-- .project
-- .classpath
From what I've read in the web, I don't need to add the .metadata directory to the repository. Also, I know I could have dependency problems in some situations if I add the .classpath folder to the repository, but I prefer to not support these problematic situations (eg different Java compilers) than forcing the dev to manually set the classpath. Finally, I don't want to use Maven in this project.
Concisely, my requirements are
- I don't want to put Eclipse project files in the root folder
- I don't want to duplicate the code inside eclipse folder (that's what happens when I try to import existing code into an isolated project folder). I want it to reference the source files I have in the folder named 'src'
- I want to configure Eclipse to put the compilation output (.class files) in the 'bin' folder.
That latter item I was able to set when creating the project, but I wasn't able to configure Eclipse to reference the source code (not duplicate it) without choosing a source parent directory as the project folder.
Any help is much appreciated.
You will need to create a project in the place you want it, and then use 'linked source' directories to point to your actual source code.
more information:
Store eclipse .project files outside the project directory
Create your directory structure above with the eclipse subdirectory.
Create a new Dynamic Web Application (or whatever, assuming based on .settings)
Uncheck Use default location and specify your eclipse directory, hit Next
Remove the lone source folder
Leave Generate web.xml... unchecked
File > New > Folder and click Advanced>>
Use Link to alternate location (Linked Folder) to create src and bin directories that are linked to the top-level src and bin.
Right-click on the project and use File > New > Source folder to add the linked src folder with an output directory of bin
Move WebContent to WebContent.bak and create a new WebContent directory that is linked to the top level.
Move the contents of WebContent.bak to WebContent
Manually edit eclipse/.settings/org.eclipse.wst.common.component to change WebContent.bak to WebContent (is there a setting for this? couldn't find it)
Refresh the project
Right-click the project and select Java EE Tools > Generate Deployment Descriptor Stub

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