I'm doing a simple Java project on Netbeans. And I'm stuck not knowing how to add my own image into the code. It kept throwing IOException no matter where I put the image. This sounds ridiculous but it really got me stuck.
If you run the program from netbeans, the root folder should be the same as the root of the project folder. That is, the folder which contains build.xml and manifest.mf.
If putting your image directly in that folder, and loading it without any path given doesn't work, then you will have too look at your code and see if you can find any errors.
Try looking at you project directory and find where the html/xhtml files are. Mostly it will be located inside the web directory within the project directory projectname/build/web. That will be source for the image tag in your html page you can directly give the name of the image for "src".
Related
I am using JetBrains IntelliJ IDEA IDE. This is what I used to generate the jar file. Running the jar file from the IDE, everything looks fine.
Running the jar from the terminal, none of the images are loaded.
My feeling is, from reading around on this, that this has something to do with the relative paths used for the images... but I can't figure this out. I've tried various different project folder structures suggested on the JetBrains forums and StackOverflow, to no avail. Everything is fine until I run a jar outside of the IDE.
My current project structure:
How on Earth do I create a jar file that works everywhere?
Ok, so here's what worked for me. I opened the project structure window (Ctrl-Alt-Shift + S) and went to the Modules tab. From there, I could easily select a folder from the list and click to make it a Resource directory. I was then able to access the resources as URL's with
URL imageUrl = ClassLoader.getSystemResource("image.png")
No need to use a path to the image, just it's actual name.ext
Using IntelliJ's resource folders is probably the right way to go.
Simply right click your res folder, go to Mark Directory As and select Resources.
Then you can access files in this folder simply by name (without a res/ prefix.)
Working visual example:
Source and Resource roots are handled differently.
All the files from the Resource directories will be always copied to the output path. As suggested in another answer, one of the solutions is to configure this directory as Resources root. It's the preferred way to get it working.
If you want files from the Source roots to be copied to the output, you have to specify the patterns for the files that will be copied.
If the project is Maven or Gradle based, these patterns have no effect and IDE will use the rules of the corresponding build system to process the resources.
I've tried for hours now to find a way of getting my image to load.
I have a project with a src folder and a res folder
My code is within my src folder and I try to access all of my resources through my res folder.
However, trying to load an image proves to be an amazingly difficult feat as nothing from the various sources that have offered help have worked.
Image undefinedIcon = new Image(getClass().getResourceAsStream("/res/ids/no.png"));
This is the closest I would say I have gotten to having my resource load, but still to no avail.
Can anyone shed any light on this issue? Thanks a lot.
Here is a recipe to get this working in a typical project setup:
Make sure both your "src" folder and your "res" folder are source folders and not just some folder within your project. (This is the Eclipse terminology. I don't know how this is called in other IDEs.)
Change the argument of getResourceAsStream to "/ids/no.png".
Now loading your image should work. It has the same effect as if you would copy your "ids" folder into the source folder.
How can I add an external folder into a Netbeans project? In my case I need to add an images folder from an network drive.
In Eclipse I do this by adding a Linked Folder. What is the similar procedure in Netbeans?
Currently I'm using Netbeans 8 and created a web java project (Ant).
I can do this by adding the folder content on the build ant script or by creating a symbolic link to the specific path. Both operation are time consuming... Probably there is a (hidden) easy way to do this on Netbeans. Help please
Thanks
I would recommend simply making a Resources folder in the same directory as your src folder. Place your images in that folder, and ensure you specify the filepath accordingly when using the images in your Java project, and you should be fine.
If in your Java project you intend on iterating over images or something like that and the images will be changing on the network drive, you can always use some kind of folder synchronization method like using a briefcase. Then you can create a briefcase folder for your external folder within your Netbeans directory, and use something like auto-scanning of resources to ensure your application continues to use the updated folder.
There may be a much simpler way but this method seems feasible IMO.
The only way that I found it was by creating a symbolic link...
To create a symbolic link on windows I followed this tutorial: http://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/
Thanks all!
I guess you can go to Project Properties --> Sources --> Add Folder
here you can select your folder to add..
I created a folder with the same name and the same path as the one I needed from the remote server, then right click and Download.
CONTEXT FOR MY PROBLEM
I am creating a Project using java swings on netbeans IDE 7.1.2 plateform on
windows m/c.Then I'm executing the jar file created on windows m/c in a linux m/c(cent os).
In a part of my project i needed to display some pictures in run time.
PROBLEM STATEMENT
Case 1)
Now what happened is that in run time i brought those pictures from some other location to src directory of my project by executing cp(copy) command through java code.
And yes i have checked those pictures were copied inside src directory successfully.
After that when i tried to display those images in a label,pictures were not being displayed.
So i wondered why is that happening.
Case 2)
After this problem i made a change and i imported those pictures inside src folder of my project before "cleaning and building" the project on windows m/c instead of bringing them in run time on linux m/c.Then after this when i executed the jar file on linux m/c,pictures were getting displayed in label.
So only change i made was that i included the pictures in src directory during "cleaning and building" my project on windows m/c.
So what i don't understand is that
why pictures are not getting displayed in 1st case.
MY QUESTIONS AND DOUBT
1) In context to resources like files and pictures(which are being used in project and i'm not talking about source codes of project here),what exactly happens when a jar file is created ? Are those resources(files & pictures) also included in jar while building the project or not?
2) Why i'm facing such a problem ?(And i have also checked the permissions on those image files.They were exactly same in both cases.)
Any insights that any of you can provide will be deeply appreciated.
So I assume you ar doing some thing like
label.setIcon(new ImageIcon("src/path/to/image"));
or
BufferdImage img = ImageIO.read(new File("src/path/to/image"));
or worse, using absolute file references...
Netbeans will add the content of of your src directory to the resulting jar file when the project is built (excluding your source files ;)), this changes the way in which these resources need to be treated, you can no longer treat them as you would if they were files on the file system.
Once embedded within your application context (such as been included in your jar file), they become what is commonly known as embedded resources.
Instead, you need to use Class#getResource or Class#getResourceAsStream depending on your needs, for example...
label.setIcon(new ImageIcon(getClass().getResource("path/to/image")));
Updated with after more details
Image im=newImageIcon(this.getClass().getResource("/home/aman/Desktop/diya.jpg")).getImage();
Is the wrong approach for his type of file. This file is a file on the file system, instead you should be using something more like...
BufferedImage image = ImageIO.read(new File("/home/aman/Desktop/diya.jpg"));
jLabel1.setIcon(new ImageIcon(image));
Note, ImageIO.read throws an IOException, which will be expected to catch, this is useful for diagnosing missing files or bad image formats
Unless you have previously changed the visibility state of the label, there is no need to call setVisible, in fact, you're setting label invisible anyway...
I've been working on a little project that requires external images for display. I'm not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project so I can share it with others. Right now, it seems the only way I can get my images to show up is if I assign a specific folder on my hard drive and have the image paths in the code go to that.
I'm looking for a way to export the images as part of my JAR or as part of the same package so when I go to send this program to other people, I don't have to send them a separate archived folder of images. I'd also be interested in learning what I need to do to have my code reference the images within that package so they'll work without an external folder.
I have read about some kind of package system within Eclipse, but have thus far had no luck in figuring out how to use it. Could use some explicating!
Thanks in advance to anyone willing to give me their two cents.
Something I would have found useful with this answer is the following: make sure you put your images/files in the same eclipse folder (or sub-folder below) as your source code. I created a folder "images_ignored" using eclipse, added it to the build path but still it refused to be included in my JAR file (when creating an executable JAR).
Just drag the images folder into your Eclipse project, then choose to "Copy New Folder" or "Copy File and Folder" depending on Eclipse version, and then right click on the image folder (in Eclipse) and --> build path "use as source folder".
you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)
Use getResource() to load the images:
ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
If you're using JDK 1.7 or JDK 1.8, you might want to use the NIO.2 API.
for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
if ("jar".equals(provider.getScheme()))
return provider.newFileSystem((new File(Start.class
.getProtectionDomain().getCodeSource().getLocation().toURI()))
.toPath(), new HashMap<String, Object>());
}
If you enter this code into a method that returns a java.nio.file.FileSystem, you can call the method to get the FileSystem for the JAR file.
To get the path to access the files inside your JAR file, you can use the following method, which then allows you to read the files however you may want.
fileSystem.getPath("/images/image.gif")
If you would like to be able to run this in Eclipse, make sure you surround the call to the method with a try/catch IOException and assign to your FileSystem object the following.
new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.toPath().toString();
This will allow you to run your program whether it's compressed into a JAR file or not.
I recommend you get used to using NIO.2, since it is a very powerful API.
If you add a folder to build path you can retrieve the images either in eclipse and when you exported it in jar file, just remember to don't reference the image with the path like img/myImage.gif but only myImage.gif !