Search code examples
javaresourcespathjarembedded-resource

Locating resource in Jar


I am having difficulty creating the image icon. I want to package a jar and have the images available to be displayed in the gui. Both of the following throw null pointer exceptions. The path is a path directly to a package in my Eclipse project that contains the necessary images.

ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource(
   "/TicTacToe/src/edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png")
   .getPath());

and

ImageIcon icon = new ImageIcon(getClass().getResource(
   "/TicTacToe/src/edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png"));

I can't see to be able to access the appropriate package. Any suggestions?


Solution

  • ImageIcon icon = new ImageIcon(this.getClass().getClassLoader()
        .getResourceAsStream("edu/luc/tictactoe/gui/resources/images/TicTacToeOIcon.png");