JAR file not running when adding images - java

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?

Related

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.

Trouble deploying jar file with images

I am trying to deploy my Java GUI as a runnable jar file. The problem is that none of the images or even the shapes I've created with Swing & AWT show up when I run the jar file. Can anyone tell me the proper way to export a Java application with images and shapes? The following code calls in random images.
int randomImage = (int)(Math.random() * 8);
try {
image = ImageIO.read(new File("src/images/" +randomImage + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
By the time of deployment, those resources will likely become an embedded-resource. That being the case, the resource must be accessed by URL instead of File. See the info page for the tag, for a way to form an URL. Namely:
URL url = this.getClass().getResource("/path/to/the.resource");

Problem converting Java applet to Jar. Maybe Image loading problem

I'm writing an applet in eclipse and under the eclipse environment it works well.
while creating a jar file from this project, the problems start.
After testing the jar with several options, I think the problem is with loading an image from a web page.
Any Other features from the applet seems to work ok in the jar.
The code of loading image in my project looks like that:
MediaTracker mt = new MediaTracker(this);
String photo = imagePath
URL base = null;
try {
base = getDocumentBase();
}
catch (Exception e) {
}
if(base == null){
System.out.println("ERROR LOADING IMAGE");
}
Image imageBase = getImage(base,photo);
// Some code that works on the image (not relevant)
// The rest of the code
icon.setImage(image);
imageLabel.setIcon(icon);
But the jar can not load the imgae and it doesn't disply it in while running and the applet is stuck because of that. (unlike in the eclipse, which loads the image and shows it)
What could be the problem?
A second problem is that from the applet in the eclipse the loading take few seconds. Is there a way to speed things up?
Thanks for any help,
I have no idea how this could be working in Eclipse.
The problem is that getDocumentBase() returns location of a page, in which the applet is embedded (e.g. http://some.site.com/index.html), and you are trying to load a picture from that location. Obviously, there is no picture, just an html (or php) file, and the loading fails.
If your goal is to load an image from inside the jar, try:
Image img = null;
try {
img = ImageIO.read(getClass().getResource("/images/tree.png"));
} catch (IOException ex) {
System.err.println("Picture loading failed!");
}
where "/images/tree.png" is path to image file in your source tree.
EDIT: If you need just to load an image from URL, you can use:
Image img = null;
try {
img = ImageIO.read(new URL("http://some.site.com/images/tree.png"));
} catch (IOException ex) {
System.err.println("Picture loading failed!");
}
This method is a bit better than Applet.getImage(new URL(...)) - I had some problems when loading many images.

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.

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