2

I have a simple java project and I have a class called Constants where I store all my required paths in static variables, like this for example:

public static final String PLAYFIELD_SMALL_IMAGE_PATH =  Constants.class.getClassLoader().getResource("Player_Small.png").getPath();

Works fine as long as I stay in my Eclipse ;)

but when I export my program to a JAR file and start it I get an exception:

java.lang.ExceptionInInitializerError

it appears when I call the following for the first time:

Constants.PLAYFIELD_SMALL_IMAGE_PATH

If I start a second time I get this error:

java.lang.NoClassDefFound: Could not initalize class Constants

What am I actually doing wrong?

EDIT 1:

I found this one here : "NoClassDefFoundError: Could not initialize class" error

Seems to be exactly the same problem. So should I not use static variables?

EDIT 2:

If I use a static initializer block an ExceptionInInitializerError is thrown instantly. without a static initializer block I was able to get to the first menu of my program.

EDIT 3:

Maybe another hint! When I extract my jar all graphics are directly in the root-folder so i tried to use a relative path like this:

public static final String PLAYFIELD_SMALL_IMAGE_PATH = "Player_Small.png";

But if i do so my graphic isnt loaded.

Community
  • 1
  • 1

2 Answers2

2

First unpack your jar and check if all the classes are there.

You can do many things to resolve that issue, first I would recommend using maven and learn that (for starters check out Maven in 5 minutes), but if you want to stay with minimal java project than you can try the following:

Thread.currentThread().getContextClassLoader().getResourceAsStream("File1.jpg");

That should help

Kris
  • 5,714
  • 2
  • 27
  • 47
  • im actually very familiar with maven but i wont use it for such small projects. and using Thread.currentThread()..... causes the same Error – user2663778 Aug 14 '13 at 09:19
  • Did you check your jar file if it contains 'Constants' class? – Kris Aug 14 '13 at 09:22
  • Yes it contains Constants.class – user2663778 Aug 14 '13 at 09:24
  • 1
    So how did you try it, with 'Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants.File_name_blah_blah);'? Where is your file located? Maybe try to use absolute path for starters and see if that works? – Kris Aug 14 '13 at 09:33
  • 1
    I used Thread.currentThread().getContextClassLoader().getResource(Constants.FileName).getPath(); my file is located under graphics/FileName and graphics is a resource folder of my project – user2663778 Aug 14 '13 at 09:40
1

your problem is exactly that:

public static final String PLAYFIELD_SMALL_IMAGE_PATH =  Constants.class.getClassLoader().getResource("Player_Small.png").getPath();

Read this article: NoClassDefFoundError. This Article explains how to resolve your problem.

I would suggest you to take all your pathes out of your Constants class into another class without using static varibles there. And maybe use URL's instead of String-Pathes because as you mentioned you use Images. Images will need URL's to be located because they arent on a HDD. If you dont use URL's your images may not be printed.

Mulgard
  • 9,877
  • 34
  • 129
  • 232