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.
Related
I have imported my project from git, and set the Source root accordingly, as User/sharan/Chess/Chess/src, where "sharan" is my home dir. In my main class i try to access a png icon from the dir path sharan/Chess/Chess/icon/" ".png, but it does not seem to fetch the icon image. I am sure however my using of the function and the path is right, because it works fine when i used it from my other machine where i programmed this application.
I think it is because i have not configured my modules right, or not configured the icons folder. I tried marking icons directory as a Resource root but still no difference.
squares[CurrentBoard.board.get(i).x][CurrentBoard.board.get(i).y].setIcon(new ImageIcon(CurrentBoard.board.get(i).filename));
where i have defined filename as a data member for the object, that specifies the path for the png image.
e.g icons/Knight_black.png
this line^ works fine i am sure, because it worked on my other machine. But it is not fetching the image from the dir icons, however program works fine with no error.
After browsing i came across a solution in which instead of accessing files, it was recommended i used a "resources" folder in src. So after copying all my pngs to the resources folder and changing the code to:
.....new ImageIcon(getClass().getResource(CurrentBoard.board.get(i).filename)));
it works perfectly..
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'm trying to make a jar, but the program needs images. When I ran the jar, the images didn't show up. However, they did in eclipse. I used this code:
label.setIcon(new ImageIcon("res/img/icon.png"));
Then I went looking on the internet for a way to fix it. I found this question here on StackOverflow, but when i tried it in my code, it's throwing a NullPointerException (also in eclipse). This is my code now:
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
The images are in a separate folder in the package, called "res":
create a package inside your project, and name it something like "Images".
Now, add images you are using into this package.
Finally, call to these images, which are inside the package.
make sure you typed image names correctly as well
That is the best way of dealing with images
update
try
label.setIcon(new ImageIcon(getClass().getResource("res/img/icon.png")));
instead of
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
Have a look that your RES folder is actually within the eclipse SRC folder.
Sometimes, users add a folder to the eclipse project instead.
Also:
Try cleaning and rebuilding your project.
In Eclipse, open the Navigator view. Open the /bin folder of the project. Does it have the /res/img/icon.png ( comment from here )
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 !
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".