I am using IntelliJ IDEA 14 and I want to add file outside of src to the JAR file. This is my current project structure.
I want to add layout.txt and saveddata.txt to the JAR file executable. I've been googling on that for a while can't find the solution
In case you need to see my code. This is how I am reading file
private Path layoutPath = Paths.get("resources/layout.txt");
content = new String(Files.readAllBytes(layoutPath));
Here is my project structure
Create a folder called "resources" at the same level as "src"
Right click the folder, select "Mark Directory As -> Resources Root"
Make new directory with name as "resources" under your project root directory.
Right click on that directory and select "Mark Directory As" ==>"Resources Root" option.
It's still for me. I tried:
+ "Mark Directory As" ==>"Resources Root"
+ getClassLoader().getResourceAsStream()
+ getClass().getClassLoader().getResource()
and Thread.currentThread().getContextClassLoader().getResourceAsStream()
For me, the resources directory was already marked as Resources Root but the content was missing in the jar. I had to manually add the resources dir to the jar artifact using the Project Structure window.
Open Project Structures window
Select Artifacts and click on the + button and then select Directory Content
Choose resources directory
Press Apply then OK
Related
I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
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.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));
I have a project that reads graphics and sound effects from a folder labeled res. This folder is at the same directory level as the src folder. I have res marked as Resources Root.
I configured my artifact to include res as a Content Directory by way of
Project Structure -> Artifacts -> Add (Alt + Insert) -> JAR
Then, in the output layout tab, I pressed the Add dropdown box -> Directory Content -> res
When I run the project in Intellij, it finds the resources just fine. Here is a code snippet showing how I access the resources:
public static final Sfx VOID_SOUND = new Sfx(TinySound.loadSound(new File("res/audio/sfx/void_sound.wav")));
However, when I build the standalone artifact, then run the jar, it looks for the resource by prepending the parent directory of my machine to the file path, all the way to root. Here is how the terminal output looks:
/home/user/Demo/res/audio/sfx/void_sound.wav (No such file or directory)
Error getting resource stream!
Is there a way to configure the artifact build to where it looks in the proper place? I want the resources included inside the jar file so it acts as a standalone jar with no external dependencies.
I'm not familiar with Linux so please take this with a grain of salt. But to me, it looks like its trying to access the file from where it doesn't have permission. Have you tried moving the file or directory to a shared location where any user can access it?
All the files from resource/source folders inside your module's Content root are included into artifact on artifact build. You can explore the artifact content produced (the artifact is placed into a directory specified in Output directory field in Artifct's Generat Settings) yourself.
Make sure you are specifying the correct directory for the file. E.g. if the res folder is marked as the module's resource root the file path name in the artifact will start from the sub-folder inside the res directory, that is audio/sfx/void_sound.wav.
Now I understand why it doesn't work. It's because resources included within the JAR artifact must be included in the classpath and are accessible using getResource(). Resources outside the classpath must be accessed using a filepath. If you use a relative path, it will resolve to where the pwd where the JAR is launched form, not to inside the JAR, hence the reason it resolves back to root. The solution is to put the resources in the classpath and access them using getClass().getResource() or getClass().getLoader().getResource().
In my project on eclipse I created another source folder to hold an mp3 file. Does anyone know how to access that file. What would the file path be? The reason why I did this is so this file would be included with my project when I turn it into a jar file and run it on different computers. If anyone has a better idea on how to do this feel free to contribute!
right click on your project > properties > resources > location
if you want to programmatically go there generally eclipse starts jvm with working directory set to base of project so you can navigate from new File(".")
Check the getResourceAsStream method from class Class: http://www.tutorialspoint.com/java/lang/class_getresourceasstream.htm
In spring:
ResourceLoader resourceLoader;
final Resource resource =
resourceLoader.getResource("classpath:com/XX/XX/" + filename);
in Eclipse, right-click on your project and select "Properties".
With "Resource" selected, it shows "Location:" which is the path to your project.
Let's say if you added a folder to your project called Audio, then you could access a mp file like this:
new File(".\\Audio\\MyAudioFile.mp3")...
While creating one folder in src directory in one project in eclipse, it makes that folder a package. Is there any way to avoid this folder from automatically being a package?
e.g., I add main folder in src directory. I don't want it to become a package. How can I do this?
Suppose I add folders in this manner: src/main/org/apache. I don't want main.org.apache to be a package, instead, I want the packaging to start from org. (i.e., org.apache).
Eclipse forces you distinguish between source directories and ordinary folders. Any subdirectories in a source folder will be considered a package.
In your case, you can create an ordinary folder outside of src/ to prevent the subdirectories from being interpreted as packages.
Alternatively, you can modify the project properties to have src/ be considered an ordinary directory, and put a source directory within it.
You can manage which directories in a project are considered source directories by:
Right-clicking your project, then click Properties.
In the left pane, click Java Build Path. In the right pane, select the Source tab.
Here you can add/edit/remove source folders.
You need to go to Project -> Properties -> Java Build Path -> Source (tab).
Remove src from "Source Folders on Build Path"
Then add src/main as a source folder. If you already have org under main, then your packages should start with org as desired.
I added the "Excluded" pattern with value ** to 'Java Build Path -> Source - > src/main/resources' and my package became a simple folder in Eclipse.
When you run a Java project in eclipse, the current working directory is the root folder of the project. So why not just create your resource folder there?
ProjectDirectory
../bin
../src
../resourceDirectory
Then, in your project, you can fetch resourceDirectory with
public File getDirectory(String dir) {
File cwdir = new File(".").getParentFile(); // remove the "." in path
for (File f : cwdir.listFiles()) {
if (f.isDirectory() && f.getName().equals(dir)) {
return f;
}
}
return null;
}
and
File resourceDir = getDirectory("resourceDirectory");
if (null == resourceDir) {
System.err.println("Resource directory not found");
System.exit(-1);
}
Note : you might perhaps want to create an utility class for the getDirectory() method, or something similar. This is just an example.
Bottom line is that the application will most likely be launched where it should be and you might just use a startup file (.bat, .sh, etc.) to do that anyway. There is no need of putting resource directories inside your source or binary folder; keep them separated.
Any folder in src directory becomes a package. If you wish to have a main folder then create a source folder of name main and then create the required package in main folder.
I need to change the name of my application, so people who download and install it can see the name but I don't know where to do it.
What file do I need to edit?
you must right click on your project in Project window [ctrl + 1] and choose rename option for
renaming your project name.
if your project is mobile Project you must right click on project name , select properties options and then on Application Descriptor edit MIDlet-name value.
If you use gradle:
BuildScripts/Project/settings.gradle
rootProject.name = 'yourprojectname'
If your project is Maven based, you can either add/update the application name in pom.xml (found in Project Files folder) or go to File >> Project Properties and add/update the application name.
Open "project.xml" file under nbproject folder inside the project folder of the one you want to change the name.
it is in the tag <name>.
Just change it and that is it.
open project.properties in dir nbproject
look up to manifest.midlets and change the name after MIDlet
Rightclick on the project node and select rename.
If you want the folder of the project also to have the new name or whatever that wasnt renamd then :
1.in build.xml in text editor find old name and replace with new one. Its place is: C:\NBProjectsFolder\OldName\build.xml Save that.
2. Remove all cash situated in , e.g.: C:\NBProjectsFolder\OldName\build\gwt-unitCache.
3. Find in C:\NBProjectsFolder\OldName\nbproject\private private.xml file and edit it in a text redactor by renaming old name within the file. Save that.
Do all the changes when the Netbeans was closed.
After opening the Ntbeans it may ask to reimport the project and all files you changed will be recognized/recreated automatically