Unable to display an image from within Jar file [duplicate] - java

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.

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

Unable to access resources outside the package in 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".

How to add bufferedImage from res file to JPanel [duplicate]

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.

JAR file not running when adding images

I was having problems exporting the pictures into the JAR file and after some browsing i found some sollution, but it doesn't seem to be working
try {
//leftFoot = ImageIO.read(new File("resources/leftFoot.png"));
//rightFoot = ImageIO.read(new File("resources/rightFoot.png"));
URL url = this.getClass().getResource("/resources/leftFoot.png");
leftFoot = ImageIO.read(url);
url = this.getClass().getResource("/resources/rightFoot.png");
rightFoot = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
}
So, if i try to upload the items from the first two lines(and comment the following 4), the main GUI stuff runs in the JAR file but the images ar not shown. If i do it the other way round(the code above), no GUI element works in the jar file. I have a "resources" folder containing the pictures both in the "bin" folder and in the main folder along bin,src,settings etc. cause i read somewhere that this is how it supposed to be, although i do not know why. leftFoot and rightFoot are BufferedImage objects. Could you tell me what is not working here?

How to access resources in JAR file?

I have a Java project with a toolbar, and the toolbar has icons on it. These icons are stored in a folder called resources/, so for example the path might be "resources/icon1.png". This folder is located in my src directory, so when it is compiled the folder is copied into bin/
I'm using the following code to access the resources.
protected AbstractButton makeToolbarButton(String imageName, String actionCommand, String toolTipText,
String altText, boolean toggleButton) {
String imgLocation = imageName;
InputStream imageStream = getClass().getResourceAsStream(imgLocation);
AbstractButton button;
if (toggleButton)
button = new JToggleButton();
else
button = new JButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
button.addActionListener(listenerClass);
if (imageStream != null) { // image found
try {
byte abyte0[] = new byte[imageStream.available()];
imageStream.read(abyte0);
(button).setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(abyte0)));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
imageStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else { // no image found
(button).setText(altText);
System.err.println("Resource not found: " + imgLocation);
}
return button;
}
(imageName will be "resources/icon1.png" etc). This works fine when run in Eclipse. However, when I export a runnable JAR from Eclipse, the icons are not found.
I opened the JAR file and the resources folder is there. I've tried everything, moving the folder, altering the JAR file etc, but I cannot get the icons to show up.
Does anyone know what I'm doing wrong?
(As a side question, is there any file monitor that can work with JAR files? When path problems arise I usually just open FileMon to see what's going on, but it just shows up as accessing the JAR file in this case)
Thank you.
I see two problems with your code:
getClass().getResourceAsStream(imgLocation);
This assumes that the image file is in the same folder as the .class file of the class this code is from, not in a separate resources folder. Try this instead:
getClass().getClassLoader().getResourceAsStream("resources/"+imgLocation);
Another problem:
byte abyte0[] = new byte[imageStream.available()];
The method InputStream.available() does not return the total number of bytes in the stream! It returns the number of bytes available without blocking, which is often much less.
You have to write a loop to copy the bytes to a temporary ByteArrayOutputStream until the end of the stream is reached. Alternatively, use getResource() and the createImage() method that takes an URL parameter.
To load an image from a JAR resource use the following code:
Toolkit tk = Toolkit.getDefaultToolkit();
URL url = getClass().getResource("path/to/img.png");
Image img = tk.createImage(url);
tk.prepareImage(img, -1, -1, null);
The section from the Swing tutorial on How to Use Icons shows you how to create a URL and read the Icon in two statements.
For example in a NetBeans project, create a resources folder in the src folder. Put your images (jpg, ...) in there.
Whether you use ImageIO or Toolkit (including getResource), you must include a leading / in your path to the image file:
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/agfa_icon.jpg"));
setIconImage(image);
If this code is inside your JFrame class, the image is added to the frame as an icon in your title bar.

Categories

Resources