I am trying to export my project as a runnable jar. I can do it using Eclipse feature or Maven, no matter.
My program is using some relatives path to load resources (images). I specify the path in eclipse from the project base directory. Everything works fine when executing my program with Eclipse, but when I generate a jar and try to run it, there is nothing and it imediatly quit. I think my program can't access my resources when executing from jar.
I have the following arborescence:
src/main/java/nantel/java/boulder/views/MainWindow.java
src/main/resources/logo.png
I was using the following to load my picture:
panel.add(new JLabel(new ImageIcon("./src/main/resources/logo.png")));
As I said, it's ok when I launch it from Eclipse but not from executable jar... I think it's a problem of classpath.
I have tried to use the following instead:
panel.add(new JLabel(new ImageIcon(getClass().getResource("logo.png"))));
I have tried different paths, but I always get a NullPointerException
(the getResource
method always return null).
Have you any idea about how I can get my resources using the getClass().getResource()
method and if it will help me running an executable jar?
I also give you my maven build configuration..
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>nantel.java.boulder.Launcher</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
When I extract the JAR archive, I can see the arborescence is not the same as in my Eclipse project. There is not longer src/main/resources or src/main/java but every thing are directly put as:
./logo.png
./nantel/java/boulder/...
And it seems logic my application can not find the resource the given relative path ("./src/main/resources/logo.png"). Any idea?
----- EDIT
The NullPointerException has been solved in the Eclipse launch, but I still have an error when trying to execute the jar.
hiveship:Desktop Maelig$ java -jar test.jar
@@@ default folder path -> file:/Users/Maelig/Desktop/test.jar!/levels/
Exception in thread "main" java.lang.NullPointerException
Why is the path like this ?? In my code:
public static final String DEFAULT_FOLDER_PATH = SpriteRepository.class.getResource("/levels/").getPath();