murkr
murkr

Reputation: 694

Eclipse doesn't correctly export resources

When I run my program from Java, everything is fine. If I export my program to a runable .jar, the program doesn't start. It does start thou, when I copy the resource folder with my pics NEXT TO the runable .jar. The resource folder is usually in my src folder. The runnable jar does include a resource folder with the pics. Its next to the folder with the package name.

I used

Icon cancelIcon = new ImageIcon(this.getClass().getClassLoader().getResource("resources/cancelIcon.png"));

and

Icon cancelIcon = new ImageIcon(this.getClass().getResource("/resources/cancelIcon.png"));

Why doesn't my program work after export?

Upvotes: 1

Views: 744

Answers (2)

Iamat8
Iamat8

Reputation: 3906

It does start thou, when I copy the resource folder with my pics NEXT TO the runable .jar.

This is because when you export runnable .jar it gives you three option under Library handling

  • Extract required libraries into generated JAR
  • Package required libraries into generated JAR
  • Copy required libraries into a sub-folder next to the generated JAR

you might have selected 3rd one i.e why when you copy sub-folder next to .jar it run properly....

I would suggest to select 1st (Extract ...) while exporting and try again to run your .jar....

Your resources folder (containing image) should be inside your src folder...and to set imageIcon in java you can use Toolkit like this..

public class Main extends JFrame{
  private JPanel MainPanel;

  public static void main (String[] args){      //--main method --//
      Main window = new Main();
      window.setVisible(true);
  }

  private Main(){                               //--constructor --//

      MainPanel = new JPanel(null);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("resources/cancelIcon.png")));

      this.add(MainPanel);
  }
}

Hope it helps..

Upvotes: 0

murkr
murkr

Reputation: 694

Problem was, that eclipse doesn't care about case sensitivity in paths, but the exported jar does. I had a file "animation.GIF" and in the sourcecode stood "animation.gif". Eclipse didn't care, the jar did.

Upvotes: 2

Related Questions