I am trying to load an image into a JLabel using:
java.net.URL imgURL =Thread.currentThread().getContextClassLoader().getResource("MyLogo.png");
However, I always get a null pointer. I have tried every combination of my folder structure I could think of.
This is where file is on my c: drive, but I want to run as webapp.
C:\Users\JoJo\Eclipse\workspace1\proj1\src\main\resources\Images\MyLogo.png
I have tried things like:
src/main/resources/Images/MyLogo.png
java.net.URL imgURL =Thread.currentThread().getContextClassLoader().getResource("/main/resources/Images/MyLogo.png");
That should do it.
As far as I know, The first / in the path indicates it should search inside the .jar file.
you need to add the directory to the class path
my advice is to add another folder in proj1 called resources and copy the image into it like so:
C:\Users\JoJo\Eclipse\workspace1\proj1\resources\Images\MyLogo.png
then you go in the project settings and set the classpath to include the C:\Users\JoJo\Eclipse\workspace1\proj1\resources\ folder, then you can use /Images/MyLogo.png
Related
[Java]
I am having trouble figuring which String to put inside variable s below.
return new ImageIcon(getClass().getResource(s));
I do know that I need to put the path here, but I don't know the format.
This is the full path of the image I am trying to import (which didn't work):
Users/Kevin1031/eclipse-workspace/B-Tring/bin/textures/Center_Ring_0.png
I am using Mac OS, and B-Tring is the project folder, textures is the package, and Center_Ring_0.png is the image I want to import and use.
So, can someone tell me what path to put in?
first try the full absolute path with / at the beginning
/Users/Kevin1031/eclipse-workspace/B-Tring/bin/textures/Center_Ring_0.png
I don't know about Eclipse, I work in IntelliJ, where if you cofigure a folder as "resources" in the module settings, you can reference from there. f.e. if you would set bin/ as resourcer the path would be /textures/Center_Ring_0.png.
Third option is to use a relative path. f.e. if you call the method from B-Tring/src/foo.java try something like ../textures/Center_Ring_0.png. Notice not starting with the slash
I'm trying to st the icon on a Jlabel but I get "NullPointerException" every time I run it. It does run while I put in the full path but I don't want to do that because I want to move the java programme around the environment.
jLabel1.setIcon(new ImageIcon(this.getClass().getResource("/data/images/image.jpg")));
I believe the problem is in the path I'm trying to use.
My rough project environment is:
projectfolder/src
projectfolder/data/images/image.jpg
I've tried using:
/image.jpg
/data/images/image.jpg
data/images/image.jpg
.\\data\\images\\image.jpg
What am I doing wrong?
Class#getResource() returns the resource relative to your class' location. Use ClassLoader#getResource() instead. If using the default class loader, it returns the resource relative to the classpath of your program.
this.getClass().getClassLoader().getResource("data/images/image.jpg")
You should place your data folder inside the src folder. Otherwise the data folder is not contained in the program's .jar.
How to set the images in background of shell using relative path. I have image which resides in folder "res". I am adding screen shot here, just you to have better understanding of 'directory structure' as well as my question(in case if it seemed little ambiguous to you). Any suggestion and help will be appreciated.
The SplashWindow.jpg is placed next to the class notDltNow which you specify as the reference for relative paths, so you can simply write:
shell.setBackgroundImage(SWTResourceManager.getImage(
notDltNow.class, "SplashWindow.jpg"));
It is also a good idea to put resources inside the src folder so when you export your code e.g. into a jar, the resources will also be exported and still available to the code without additional hassle.
In order to read your images from resources folder, you need to :
add res folder to classpath
locate file absolutely, i.e. "/res/SplashWindow.jpg"
Hope this helps
I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to "/bin/stuff/icons/image.png" , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path)
BufferedImage image = ImageIO.read(in)
Where path is
"projectName/resourceFolder/" + nameOfResource.stuff
as found in the src directory of the eclipse project.
E.g.
"myProject/images/icon.png"
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And - no - I don't save images in the bin/ - but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.
Don't put anything under the bin directory in Eclipse: if you run a clean on the project it will be erased.
What you can do is to define a new source folder like resources, and put the image there. This way it will be automatically copied to the bin folder.
If you include the resources folder into the Jar, it will be available in both environments by using something like:
ImageIO.read( getClass().getResource("/image.png") )
PS: You can evade using a different resources folder but mixing the sources and images will quickly pollute your source folder.
BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"));
First of all, yes I did add the image folder to my classpath.
For this I receive the error java.lang.IllegalArgumentException: input == null!
I don't understand why the above code doesn't work. From everything I read, I don't see why it wouldn't. I've been told I should be using FileInputStream instead of GetResourceAsStream, but, as I just said, I don't see why. I've read documentation on the methods and various guides and this seems like it would work.
Edit: Okay, trying to clear some things up with regards to what I have in the classpath.
This is a project created in Eclipse. Everything is in the project folder DreamGame, including the "Images" folder. DreamGame is, of course, in the classpath. I know this works because I'm reading a text file in /Images with info on the gif earlier on in the code.
So I have: /DreamGame/Images/player.gif
Edit 2: The line that's currently in the original post is all that's being passed; no /DreamGame/Images/player.gif, just /Images/player.gif. This is from a method in the class ImagesLoader which is called when an object from PlayerSprite is created. The main class is DreamGame. I'm running the code right from Eclipse using the Run option with no special parameters
Trying to figure out how to find which class loader is loading the class. Sorry, compared to most people I'm pretty new at this.
Okay, this is what getClassLoader() gets me: sun.misc.Launcher$AppClassLoader#4ba778
getClass().getResource(getClass().getName() + ".class") returns /home/gixugif/Documents/projects/DreamGame/bin/ImagesLoader.class
The image file is being put in bin as well. To double check I deleted the file from bin, cleaned the project, and ran it. Still having the same problem, and the image file is back in bin
Basically, Class.getResourceAsStream doesn't do what you think it does.
It tries to get a resource relative to that class's classloader - so unless you have a classloader with your filesystem root directory as its root, that won't find the file you're after.
It sounds like you should quite possibly really have something like:
BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"))
(EDIT: The original code shown was different, and had a full file system path.)
and you make sure that the images are copied into an appropriate place for the classloader of the current class to pick up the Images directory. When you package it into a jar file, you'd want the Images directory in there too.
EDIT: This bit may be the problem:
First of all, yes I did add the image folder to my classpath.
The images folder shouldn't be in the classpath - the parent of the Images folder should be, so that then when the classloader looks for an Images directory, it will find it under its root.
If you use resourceAsStream "/" referes to the root of the classpath entry, not to the root of the file system. looking at the path you are using this might be the reason.
If you load something from some home path you probably should use a FileInputStream. getResourceAsStream is for stuff that you deploy with your app.