3

i am having a issue with exporting my java file to a runnable jar file. In the eclipse it works fine, but when i exported it, it doesn't work.

I already tried the (java -jar MyJar.jar) to get the log but it says "Unnable to access the jar file"

I think the problem is because of this:

java.net.URL logoOneUrl = getClass().getResource("/logo.png"); //already tried without the "/" and it doesn't work withouth the "/"
Icon logoOne = new ImageIcon(logoOneUrl)

Because when i put it in comment // when I exported it runs but without the image.

I also tried this way: java.net.URL logoScience = getClassLoader().getResource("logo.png"); but it doesn't work too. What am i doing wrong? How do i export a runnable jar file with a image on a JLabel?

(This is what i have on my JLabels) JLabel lblImgLogin = new JLabel(logoOne);

UPDATE

ImageIcon icon = createImageIcon("/logo.png","a pretty but meaningless splat");

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; }

JLabel lblImgLogin = new JLabel(icon);

Same problem, Works on eclipse but not working when exported to runnable jar file

Firmeza
  • 127
  • 2
  • 12

2 Answers2

4

This one is correct:

enter image description here

public class MainApp {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Testing");
        ImageIcon icon = createImageIcon("/resources/logo.png");
        JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER);

        //Set the position of the text, relative to the icon:
        label1.setVerticalTextPosition(JLabel.BOTTOM);
        label1.setHorizontalTextPosition(JLabel.CENTER);

        frame.getContentPane().add(label1);
        frame.pack();
        frame.setVisible(true);
    }

    protected static ImageIcon createImageIcon(String path) {
        URL imgURL = MainApp.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

enter image description here

enter image description here

aw-think
  • 4,723
  • 2
  • 21
  • 42
  • First problem is that, keep a slash before images. make it "/images/logo.png" instead of "images/logo.png". Second, better use the images in the "Resources" folder. – Saqib Rezwan Feb 11 '15 at 12:05
  • @SaqibRezwan What you answered and commented, didn't work at all. – Firmeza Feb 11 '15 at 12:14
  • @SaqibRezwan: Maybe it is a little problem in the getResource() method. But if the picture is located in the resources folder it runs perfect. If you make your own folder like "images" it breaks. ? But your Code runs too. – aw-think Feb 11 '15 at 12:22
  • Thats I was trying to tell in my answer. – Saqib Rezwan Feb 11 '15 at 12:29
1

Create a folder name 'Resources'(exactly this name). Now keep the logo.png in the Resources folder. Now use code as following.

ImageIcon imageIcon = new ImageIcon(getClass().getResource("/Resources/logo.png"))

Compile and build jar (Use 'package required libraries for generated jar' for Eclipse export). If after extractions of the jar, you get the Resources folder and files under it, then hopefully it will work. Sorry for my bad English

Saqib Rezwan
  • 1,382
  • 14
  • 20
  • java.lang.NullPointerException at javax.swing.ImageIcon.(Unknown Source) Doens't compile/ run – Firmeza Feb 11 '15 at 11:51
  • The path is not correct. Where did you create the Resources folder? Is it in just the project path? If the path is right, it should work. I faced this problem and applied the above procedure. Can you please give the project structure for the png file? – Saqib Rezwan Feb 11 '15 at 11:56
  • Did you use a slash (' / ') before the Resources in the code? I made this mistake. Can you give a picture of your code? also the project structure from the Eclipse? – Saqib Rezwan Feb 11 '15 at 12:11
  • The problem is fix now, but okay... - No i didn't used a slash before the Resources ` ImageIcon imageIcon = new ImageIcon(getClass().getResource("Resources/logo.png"));` – Firmeza Feb 11 '15 at 12:20