Unable to access resources outside the package in Java - java

I am currently developing a game, and have encountered a very burdensome problem.
I want to draw a picture on the screen but every time I try to read the resource / picture I get NULL. I tried 2 kinds of methods to read the picture but still couldn't.
But when I moved the image into the package that contains the class from which I was trying to read the image the image appeared.
So the problem is that i just can't access resources outside of the current package. And i need to know how can i do that, how can i access this resource. It has to be a resource that I can use even after exporting the game to a JAR file.
Code I tried (The first one is buffered image and the second is just image type:
try {
image = ImageIO.read(getClass().getResource("blocks.png"));
} catch (IOException e) {
e.printStackTrace();
}
--------------------------------------------------------------
image = new ImageIcon(getClass().getResource("blocks.png))).getImage();
I hope the question is clear enough.

Try creating a package called "resources", instead of a folder, and then access your image as "resources/blocks.png".

Related

Image won't show up when using JAR file [duplicate]

This question already has answers here:
Jar get image as resource
(3 answers)
Closed 2 years ago.
So I'm using an image for the chess pieces in this basic chess game I'm making.
When I run the program in Eclipse, it works perfectly fine as expected. But when I use Eclipse to export and then run that program, it gives the error java.imageio.IIOException: Can't read input file!
The image is stored in the source folder in a package names images.
I load the image using
BufferedImage image = null;
try {
image = ImageIO.read(new File("src/images/Chess_Pieces.png"));
} catch (IOException e) {
System.out.println(e);
}
I've tried locating the image to many different places and I've tried different ways of loading the image, none of them work and I have made sure that the image actually appears correctly in the exported JAR file.
Change it:
image = ImageIO.read(getClass().getResource("/images/Chess_Pieces.png"));
See :
Different ways of loading a file as an InputStream
Loading image resource

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.

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.

Changing the location of files in Java application

I've been putting all of my images for my Java application in a package called "rtype" inside src where I also also have my Class that deals with these images. I wanted to sort the images and put them in a folder of their own. When I do this, The images will no longer load into the class, and I know it's because I changed the file path. I've done some research and tried a few different things. This is basically what I had originally:
String walkingDown = "WalkingDown.gif";
ImageIcon ii;
Image image;
ii = new ImageIcon(this.getClass().getResource(walkingDown));
image = ii.getImage();
and It worked just fine before I moved the location of the images outside the location of the class. Now it cant find the images. Here is what I tried and found online to try (The folders Name is Sprites):
//use getClassLoader() inbetween to find out where exactly the file is
ii = new ImageIcon(this.getClass().getClassLoader().getResource(standingDown));
and
//Changing the path
String walkingDown = "src\\Sprites\\WalkingDown.gif";
//also tried a variation of other paths with no luck
I am using the C drive, but don't want to use "C" in my extension, as I want it to be accessible no matter where I put the project. I am fairly stuck at this point and have done enough looking into it to realize that It was time to ask.
I have a separate "package" for images with that name (in the src folder)
Try something like this:
try {
ClassLoader cl = this.getClass().getClassLoader();
ImageIcon img = new ImageIcon(cl.getResource("images/WalkingDown.gif"));
}
catch(Exception imageOops) {
System.out.println("Could not load program icon.");
System.out.println(imageOops);
}
Your variable is named walkingDown, but you pass in standingDown to the getResource() method.
new ImageIcon("src/Sprites/WalkingDown.gif");

Categories

Resources