ImageIcon not displayed after creating jar - java

i'm trying to make a small project in java with no luck
if i'm compiling the program with eclipse everything is good, but when i'm i creating the jar file i get a blank window
this is the image i declared for:
public ImageIcon BACKGROUND = new ImageIcon() ;
I have tried to do the following stuff:
1.
new ImageIcon("Images/wood.jpg").getImage());
2.
this.BACKGROUND.setImage(Toolkit.getDefaultToolkit().getImage("Images/wood.jpg"));
3.
this.BACKGROUND = new ImageIcon(getClass().getResource("Images/wood.jpg"));
4.
/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
1 & 2 showing the images after compiling and 3 & 4 returns null
another think is that i'm using mac and when i'm working with windows no image is displayed after compiling.

Here's a small working example if that helps:
public class ImageIconApplet extends JApplet {
public void init() {
URL url = getClass().getResource("/images/WhiteFang34.jpg");
ImageIcon icon = new ImageIcon(url);
JLabel label = new JLabel(icon, JLabel.CENTER);
add(label);
}
}
The jar for the applet on that page contains two files:
/com/whitefang34/ImageIconApplet.class
/images/WhiteFang34.jpg
I'm not sure if you're deploying an applet or a desktop swing app, however the code to load images and the packaging requirements are the same either way.

I have something...
ImageIcon icon = new ImageIcon(getClass().getResource("/package/image.png"));
JFrame.setIconImage(icon.getImage());
Place this inside your constructor.

Are you sure Images/wood.jpg is present in the .jar file?
I suggest you unzip the jar file and make sure that it's there. Otherwise you'll have to revise your build scripts (or what ever technique you use) that builds the jar.
Related questions:
adding non-code resources to jar file using Ant
Add image to JAR Java

Related

Java in netbeans, read image file from java package

I wonder if anyone could help? I have been developing a java app which displays images from disk on a jframe.
Using netbeans I have created a java package called images within my project and stored the images in that package. First off is that the way I should do it If I want them to ship with the app?
I have the following function to read the image:
Image readImg(String file)
{
Image image = null;
try {
File imgFile = new File(file);
image = ImageIO.read(imgFile);
} catch (IOException e) {
// System.out.println("Can not Display Image");
e.printStackTrace();
}
if (image != null){
return image;
} else{
return null;
}
}
My problem comes that I cannot work out the relative filepath to pass to the function. I have got the image to display calling it like so:
Image headerImg = readImg("C:\\Users\\D#nb0y\\Documents\\NetBeansProjects\\GiftAidApp\\src\\images\\galogo.png");
Obviously this will completely fall apart if the app is run on any other device. Can anyone give me a heads up as to making this code portable?
thanks very much
Since it's a JFrame you're trying to add an image to, it might be better to use an ImageIcon instead of an Image.
Try this:
ImageIcon image = new ImageIcon("images/galogo.png");
JLabel imageLabel = new JLabel(image);
frame.add(imageLabel);
This should make the app portable as well if you include you images folder along with your project.

How to add a image file into the Java Swing Frame?

I tried to add an jpg image file into my Java Swing page,
but when running, system always back error "Source not found" and stucking there.
the source code are following
this.setLayout( new BorderLayout( ) );
URL url = getClass().getResource("logo");
ImageIcon imageicon = new ImageIcon( url );
JLabel label = new JLabel( imageicon );
this.add( label, BorderLayout.NORTH );
The file name is: "unStudent.java", and image file is "logo". I have put the both files in the same folder, why system can not find image file? what should I change?
Thanks in advance.
Tony
Try this method to create your ImageIcon. Make it public or private, it's up to your design.
private ImageIcon createIcon(String path)
{
URL url = getClass().getResource(path);
if(url == null)
{
System.err.println("Could not load the icon: " + path);
}
ImageIcon icon = new ImageIcon(url);
return icon;
}
now make sure that path is the correct path to the file and with the file extension.
I'm quite sure your mistake, or one of your mistakes, is the file extension missing. So if your logo file is a png image type and it is stored in c:\images your path must be c:\images\logo.png
I would recommend you to place your images inside your project folder.
PS; pay attention to \ escaping. I assume you know it.
Try this soluion:
URL url = getClass().getResource("logo.jpg");
if it does not works try to put the absolute path of your image as follows:
URL url = new URL("C://folder/logo.jpg");

Load images in jar file

I'm trying to load an image from an executable JAR file.
I've followed the information from here, then the information from here.
This is the function to retrieve the images:
public static ImageIcon loadImage(String fileName, Object o) {
BufferedImage buff = null;
try {
buff = ImageIO.read(o.getClass().getResource(fileName));
// Also tried getResourceAsStream
} catch (IOException e) {
e.printStackTrace();
return null;
}
if (buff == null) {
System.out.println("Image Null");
return null;
}
return new ImageIcon(buff);
}
And this is how it's being called:
logo = FileConverter.loadImage("/pictures/Logo1.png", this);
JFrame.setIconImage(logo.getImage());
With this being a simple Object.
I'm also not getting a NullPointerException unless it is being masked by the UI.
I checked the JAR file and the image is at:
/pictures/Logo1.png
This current code works both in eclipse and when it's been exported to a JAR and run in a terminal, but doesn't work when the JAR is double clicked, in which case the icon is the default JFrame icon.
Thanks for you're help. It's probably only me missing something obvious.
I had a similar problem once, which turned out to be down to issues relative addressing and my path being in the wrong place somehow. I dug this out of some old code I wrote that made it use an absolute path. That seemed to fix my problem; maybe it will work for you.
String basePath = (new File(".")).getAbsolutePath();
basePath = basePath.substring(0, basePath.length()-1);
FileConverter.loadImage(basePath+"/pictures/Logo1.png", this);

Small Java exception that I can't understand

Can you help me resolve this?
Exception in thread "main" com.jme3.asset.AssetNotFoundException: Interface/splash.png
at com.jme3.system.JmeDesktopSystem.showSettingsDialog(JmeDesktopSystem.java:112)
at com.jme3.system.JmeSystem.showSettingsDialog(JmeSystem.java:128)
at com.jme3.app.SimpleApplication.start(SimpleApplication.java:125)
at adventure.Q3World.main(Q3World.java:85)
It used to work, then I had to repackage everything and now I might've forgotten a setting or likewise in Eclipse. The splash file is there but it is not on some path.
What I'm trying to to is this, which works in my previous build:
settings.setSettingsDialogImage("Interface/splash.png");
I've also tried adding the path to the resource panel but to no other effect:
And in Java build path, the resource is listed but it's still not working:
The larger code block that I want to work and that which is working in the built jar but not from within eclipse juno is:
public static void main(String[] args) {
File file = new File("quake3level.zip");
if (!file.exists()) {
useHttp = true;
}
Q3World app = new Q3World();
AppSettings settings = new AppSettings(true);
settings.setTitle("Dungeon World");
settings.setSettingsDialogImage("Interface/splash.png");
app.setSettings(settings);
app.start();
}
Success
Can you tell us how you run your code (e.g. command line, if so, what is the classpath), and where all the locations of splash.png are in your folder structure?
The file needs to be on the classpath, as that appears to be from where this code loads the image.
http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/desktop/com/jme3/system/JmeDesktopSystem.java?spec=svn10038&r=10038#112
String iconPath = sourceSettings.getSettingsDialogImage();
if(iconPath == null){
iconPath = "";
}
final URL iconUrl = JmeSystem.class.getResource(iconPath.startsWith("/") ? iconPath : "/" + iconPath);
if (iconUrl == null) {
// *****LINE 112 below*****
throw new AssetNotFoundException(sourceSettings.getSettingsDialogImage());
}

Jar Exportation Not Showing Images

I am a new programmer to Java. I have a made a small Directory Application that I would like to export, but for some reason, whenever I try to export it to a runnable jar file, the result doesn't contain any of the images I specified within my program. Basically, I ran it in eclipse, and it worked fine, but when I ran it as an runnable JAR, it has no images. I have 5 .java files that are all bundled with eachother. My Images are found at Images/Image.png [I already made The Images folder a source folder.]
I have tried eveything, but for some reason i can't get it to work, if you have any knowledge on the topic, please tell me. I don't know if its because I'm a noob or something I'm doing wrong.
static ImageIcon logoicon = new ImageIcon("Images/Logo.png");
Here is the method I use:
public static ImageIcon createImageIcon(final String path) {
InputStream is = ImageLoader.class.getResourceAsStream(path);
int length;
try {
length = is.available();
byte[] data = new byte[length];
is.read(data);
is.close();
ImageIcon ii = new ImageIcon(data);
return ii;
} catch (IOException e) {
LogManager.logCriticalProblem("Image not found at {} - {}", new Object[]{path, e.getMessage()});
}
return null;
}
If you have problems with this method, try altering the path you're using:
"Images/Logo.png"
"/Images/Logo.png"
"src/Images/Logo.png"
"/src/Images/Logo.png"
Or other combinations depending on your package structure. For example, if your images are actually in net.blah.fizz.Images, your path would be "/net/blah/fizz/Images/image.png"
Did you try getResourceAsStream() method ? Checkout this page for more information

Categories

Resources