0

I have a maven project in eclipse with App.java in in my.package in src/main/java and a couple of pictures in my.package.resources

I looked in the jar and the structure is like this:

MyApp
|- chrriis \\ library stuff
|- com \\ stuff with google and sun
|- javax
|- META-INF \\ contains MANIFEST.MF and other maven stuff
|- org \\ contains most of my used libraries
|- my
|  |- package
|  |  | App.class
|  |  | App$1.class
|  |  |- resources
|  |  |  |-picture.png

When I load a picture like:

JPanel p1 = new JPanel(new GridBagLayout());
p1.add(new JLabel(new ImageIcon(App.class.getResource("resources"+File.separator+"picture.png"))));

and run it in eclipse, it works fine. But when I Export it to a runnable jar it give me this error:

Exception in thread "main" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        ...

But when I move the picture.png to my.package and do App.class.getResource("picture.png") it works fine.

I tried this four solutions but they did not work for me: (1) (2) (3) (4)

Any help would be appreciated.

Community
  • 1
  • 1
Selphiron
  • 897
  • 1
  • 12
  • 30
  • Why do you have the images in a separate sub folder from the class that is using them? – hotzst Jan 02 '16 at 14:25
  • Well I read that it is a better idea to have resources in another folder. But a using sub folder was the only working way for me, so that the pictures are included in the jar. – Selphiron Jan 02 '16 at 14:34
  • 1
    Then how is it that [(3)](http://stackoverflow.com/questions/28053214/eclipse-and-maven-resources-in-jar) does not work for you? Can you provide a bit more details about the above mentioned solutions and where you run into problems with it? – hotzst Jan 02 '16 at 14:40
  • 1
    Does it work in the sub-folder case if you replace `File.separator` by `"/"`? The Javadoc for Class.getResource() suggests that it expects '/' to separate folder names rather than the system-specific separator - which looks like it might be '\\' on your system. – sisyphus Jan 02 '16 at 14:52
  • @hotzst apparently I made the same mistake in all aproaches. I used either `File.seperator` or `\\`. Using `/` worked. – Selphiron Jan 02 '16 at 15:02
  • @sisyphus yep replacing `File.seperator` by `/` worked. Since I use window it used `\\` -.- thanks. – Selphiron Jan 02 '16 at 15:03

0 Answers0