Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Simple Project handling Resources
    primarykey
    data
    text
    <p>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:</p> <pre><code>public static final String PLAYFIELD_SMALL_IMAGE_PATH = Constants.class.getClassLoader().getResource("Player_Small.png").getPath(); </code></pre> <p>Works fine as long as I stay in my Eclipse ;)</p> <p>but when I export my program to a JAR file and start it I get an exception:</p> <pre><code>java.lang.ExceptionInInitializerError </code></pre> <p>it appears when I call the following for the first time:</p> <pre><code>Constants.PLAYFIELD_SMALL_IMAGE_PATH </code></pre> <p>If I start a second time I get this error:</p> <pre><code>java.lang.NoClassDefFound: Could not initalize class Constants </code></pre> <p>What am I actually doing wrong?</p> <p>EDIT 1:</p> <p>I found this one here : <a href="https://stackoverflow.com/questions/1401111/noclassdeffound-exception-could-not-initialize-class-error-help">&quot;NoClassDefFoundError: Could not initialize class&quot; error</a></p> <p>Seems to be exactly the same problem. So should I not use static variables?</p> <p>EDIT 2:</p> <p>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.</p> <p>EDIT 3:</p> <p>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:</p> <pre><code>public static final String PLAYFIELD_SMALL_IMAGE_PATH = "Player_Small.png"; </code></pre> <p>But if i do so my graphic isnt loaded. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload