Load Image from Jar: Always null - java

Looked at other posts on SO and they did not solve this issue.
I'm trying to load an image from my jar file. It is continuously coming up as null. The image is located under:
.Jar file > images > BLOCK.png
To load the image I am doing:
BufferedImage bImg;
URL url = getClass().getResource("/images/BLOCK.png");
try {
bImg = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(TileEngine.class.getName()).log(Level.SEVERE, null, ex);
}
url is null as is bImg.
Do not worry about case sensitivity as I've already checked that.

try this :
Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/BLOCK.png"));

Which jar file is the image in, compared with the class you're using to call getResource? If they're loaded by the same classloader, it should be fine.
Have you double-checked that the jar file actually contains the file?
Are you sure that your classloader is actually using the jar file at all (rather than .class files directly on disk, for example)?
If you have a short but complete program which demonstrates the problem, that would really help. (A console app would be ideal... we don't need to see the image, after all.)

Related

Having problems with packaging images into a jar file

I have scoured this site, and been looking over solutions on how to fix this, and none have given a solution that changes the error occurring. I cannot pinpoint what the problem is. None of the similar questions regarding this have helped.
I'll start with the code I have
public static BufferedImage getImage(String uri) throws IOException {
BufferedImage image = null;
image = ImageIO.read(ImageLoader.class.getResource(uri));
return image;
}
And where I call the method
try {
sprite = ImageLoader.getImage("images/testSprite.png");
} catch (IOException e) {
e.printStackTrace();
}
The error returned is IllegalArgumentException input == null on the ImageIo.read line in the original method.
To preface, I have tried every imaginable file naming sequence. I tried it including res, the name of my resource folder (both with and without a slash at the beginning), I tried it with a slash preceding images, the folder that contains the image I'm trying to access, and simply the name of the image, again, with and without the slash.
I used Eclipse's "Build Path" to create my res folder in the Project folder, but if I look at the .zip, it doesn't contain the folders I included, even the src folder, but it does contain all of the class files from the src folder. I'm confused why this is happening.
I would really like some insight on what exactly I am doing wrong here.
EDIT: After trying nearly everything imaginable, I finally figured it out. The folder needs to be created as a Package, and the image needs to be placed within the package. A source folder or normal folder doesn't seem to be picked up by the compiler. Just add a package to your source folder for the image to be loaded.

Deploying images used in Java Application with the built jar file

After using images for example on a Button, when I build the application creating the .jar file and execute only the file, the images are not there but would only show if I copy the images folder in the same directory as the jar file. Why is that and how can I resolve this if possible?
I am currently using the following code to set the icon/image:
JButton btn = new JButton("Text", "img/icon.png");
The fact that you can use the images when they are stored outside the jar, suggests that you are doing something of the kind:
File image = new File("directory/image.jpg");
InputStream is = new FileInputStream(image);
This reads a file from a directory on the file system, not from the classpath. Now, if you have packaged your image in a "directory" inside your Jar, you must load the image from the classpath.
InputStream is = getClass().getResourceAsStream("/directory/image.jpg");
(note the slash in the path)
Or
InputStream is = getClass().getClassLoader().getResourceAsStream("directory/image.jpg");
(note the absence of the slash in the path)
Your example, as it is now, should not compile. (The second argument of your JButton construtor is an Icon, not a String, java 8). So when you were getting the image from the file system, you were probably doing something else.
With your example, you need to read an image from the inputstream and convert it to an Icon:
try (InputStream is = getClass.getClassLoader().getResourcesAsStream("directory/image.jpg")) {
BufferedImage image = ImageIO.read(is);
return new JButton("Text", new ImageIcon(image));
} catch (IOException exc) {
throw new RuntimeException(exc);
}
That should use the image that is located in "directory" inside your jar. Of course, you need to include the image within your jar, or you will get a NullPointerException on the inputstream is.
I think you need to understand the
ClassLoader:
A typical strategy is to transform the name into a file name and then
read a "class file" of that name from a file system.
So with this you will be able to lead Resources of your project with getResource
public URL getResource(String name)
Finds the resource with the given name. A resource is some data
(images, audio, text, etc) that can be accessed by class code in a way
that is independent of the location of the code.

Accessing an Image outside of a jar from inside a jar

I tried searching for similar answers but I'm still facing a few difficulties. I'm trying to access a .gif from inside a jar. My file structure is:
src
|-> main
|->webapp
|->images
|-> bulb.gif
|->signed.jar
If I use an absolute file path then I can grab my image by just using return (new ImageIcon(path, description)).getImage(); but I'm trying to just use relative paths. I tried using return (new ImageIcon(getClass().getResource("/images/bulb.gif"))).getImage(); but that didn't work. Am I using the correct pathway in the latter method, or is there a better alternative?
EDIT: I also tried below with no success.
InputStream image = getClass().getClassLoader().getResourceAsStream("images/bulb.gif");
Image in = null;
try {
in = ImageIO.read(image);
}
catch(Exception e){
}
return (new ImageIcon(in, description)).getImage();
My file structure is pertaining to the question is
project/src/main/java/web/load/jar/something.java
project/src/main/java/web/load/jar/images/bulb.gif
project/build.xml
project/target/thing.signed.jar
for my jar and
mainproject/src/main/app/thing.signed.jar
mainproject/src/main/app/images/bulb.gif
for my main webapp. I was trying to access bulb.gif from my signed.jar but it might be easier to just put it in my signed.jar.

Reference resource in top directory (not package) in Java JAR/Eclipse

I'm trying to access some resources located in the top directory of my (Java) project (/resources/imgname.jpg) from a class 'GUI' located in a package 'gui'.
Originally I used the following code:
ImageIcon image = new ImageIcon("./resources/imgname.jpg");
Image img = image.getImage();
This works fine in Eclipse, but doesn't display the image when it's in a runnable JAR. So after some searching it seems you need:
InputStream resource = GUI.class.getResourceAsStream("./resources/imgname.jpg");
try {
Image image = ImageIO.read(resource);
} catch (IOException e) {//trycatch needed because of read method}
Now this doesn't work in either Eclipse or JAR.
I've tried changing reference and location, but the only way I can get the image to display is by placing it in the 'gui' package folder. So is there any way I can reference it in the top folder of the project instead (so I don't have to move the resources to 'gui')?
Thx,
Magic
Try this.
InputStream resource = GUI.class.getClassLoader().getResourceAsStream("resources/imgname.jpg");
It works for me.

how to read image from project folder in java?

I am developing web method for webservice in java. In this web method I have to read image from my images folder which resides in my webservice project folder. I am using the code as follows.
#WebMethod(operationName = "getAddvertisementImage")
public Vector getAddvertisementImage()
{
Image image = null;
Vector imageList = new Vector();
try
{
File file = new File("E:/SBTS/SBTSWebservice/web/adv_btm.jpg");
image = ImageIO.read(file);
imageList.add(image);
}
catch (IOException e)
{
e.printStackTrace();
}
return imageList;
}
I am unable to read image from images folder.I am getting error image file "input file can't read" at image = ImageIO.read(file); how to resolve this issue ? Is there any mistake in my code or is there any other way to read image ? if there is any mistake in my code then can you proide me the code or link through which i can resolve the above issue.
Is the E:\ drive mapped on your web server? The Java compiler has no idea that you might access files outside of its scope and how it could tell your web server to map a network drive or a local hard disk which is attached to your development computer.
The solution is to put the image file into the same directory as the Java source file and then use
InputStream in = getClass().getResourceAsStream("adv_btm.jpg");
Check that your IDE (or whatever you use to build your application) does copy the image file in the same directory where it creates the .class file. Then it should work.

Categories

Resources