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
Related
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".
This question already exists:
Referencing Image within JAR [duplicate]
Closed 3 years ago.
I know this has been asked before but I can't get it to work after trying all variations.I have a small program that I'm wanting the launch frame to have an image displayed on it. I can achieve this by having the jar in the same folder as the image and referencing it, however when I try to reference within the jar file itself I keep getting error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
The code I am using is this:
private void imagePanel() {
setLayout(new BorderLayout());
BufferedImage image;
try {
image = ImageIO.read(this.getClass().getResource("/src/ticketMaster/img/logo.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
add(new JLabel(new ImageIcon(image)), BorderLayout.CENTER);
}
I'm not sure if the path is incorrect, I have made a img folder inside the package named img which contains the logo. I have tried all variations of it
/img/logo.png
/ticketMaster/img/logo.png
/src/ticketMaster/img/logo.png
I am not able to get the image to load like I would just referencing the image outside of the jar. I've used resourceasstream as well and not had any luck with that.
Any ideas here?
I dont know your folder or your package structure, but you can try to remove the first '/' for the path you given in the getRessource() method, it may be looking for a absulute path.
This question already has answers here:
Can't read and write a TIFF image file using Java ImageIO standard library
(5 answers)
Closed 5 years ago.
I'm trying to read an image from a relative path:
String fp = "../resources/img/wc/text/039.tiff";
The following code succeeds:
File fi = new File(getClass().getResource(fp).getPath());
System.out.println("fi: " + fi);
if (fi.exists() && !fi.isDirectory()) {
System.out.println("file exists"); // <-- console prints this
}
try {
img = ImageIO.read(getClass().getResource(fp));
System.out.println("file read"); // <-- console prints this
} catch (IOException e) {
e.printStackTrace();
}
... but the following code just after it:
System.out.println(img.getType());
... fails, reporting:
Exception in thread "main" java.lang.NullPointerException
at com.ddc.fmwscanner.java.LoadImageApp.ddNextImage(LoadImageApp.java:60)
at com.ddc.fmwscanner.java.LoadImageApp.<init>(LoadImageApp.java:85)
at com.ddc.fmwscanner.main.FmwScanner.main(FmwScanner.java:15)
I know the image is valid, because I can open it using non-Java methods. However, those methods will not open the image from a .jar, so I need to use a pure Java method.
Any insight is appreciated.
This ended up being a problem with loading .tiff files in pure Java. Installing TwelveMonkeys ImageIO plugin did the trick. Thanks again, especially to #IlarioPierbattista, who directed me to the solution!
This question already has answers here:
Including Images with an executable jar
(2 answers)
Closed 6 years ago.
I need to display an image in a JPanel, but this image needs to be in a relative folder (my res folder) in my project path, so that my program can work on any machine, and the images are always available. The code I have for this so far is:
try {
BufferedImage image = ImageIO.read(new File("res/circle.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(image));
panel2.add(picLabel);
picLabel.setLocation(220, 180);
picLabel.setSize(100, 100);
} catch (IOException e1) {
e1.printStackTrace();
}
This code is reading the error "Can't read input file!" and I cant figure out what I'm doing wrong.
(my res folder is located in the src folder of the project)
Let's take an example. Here is a small directory structure
main
|---MyClass.java
|---myfile.png
So main is package. You will need a class to reference any resource. Here I am using MyClass.
public Image getImage() throws IOException{
return ImageIO.read(MyClass.class.getResource("myfile.png"));
}
You can use something called get current directory. This will get the folder path that the project is working off of.
You can use it like so:
System.getProperty("user.dir")
Just add the image to the correct folder and then add the path and you're golden.
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.