Java Export JAR using relative paths

I am trying to export my project as an executable jar. I can do it with Eclipse or Maven feature, it doesn't matter.

My program uses some sibling path to load resources (images). I am specifying the path in eclipse from the project base directory. Everything works fine when running my program with Eclipse, but when I create a jar and try to run it nothing happens and it clearly goes away. I think my program cannot access my resources when executed from jar.

I have the following section:

src/main/java/nantel/java/boulder/views/MainWindow.java
src/main/resources/logo.png

      

I used the following to load the image:

panel.add(new JLabel(new ImageIcon("./src/main/resources/logo.png")));

      

As I said, it's ok when I run it from Eclipse but not from the executable jar ... I think this is a class issue.

I tried using the following instead:

panel.add(new JLabel(new ImageIcon(getClass().getResource("logo.png"))));

      

I tried different paths but always get NullPointerException

(method getResource

always returns null).

Do you have any idea how I can get my resources using the method getClass().getResource()

, and if that helps me get the jar executable running?

I also give you my maven build config ..

<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 see that arborescence is not the same as in my Eclipse project. There are no more src / main / resources or src / main / java, but each thing is directly put as:

./logo.png
./nantel/java/boulder/...

      

And it looks like my boolean application cannot find the resource in the given relative path ("./src/main/resources/logo.png"). Any idea?

----- EDIT

NullPointerException was resolved in Eclipse startup, but I still have an error when trying to 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 this way? In my code:

    public static final String DEFAULT_FOLDER_PATH = SpriteRepository.class.getResource("/levels/").getPath();

      

+3


source to share


1 answer


Try adding before the presenter /

:

panel.add(new JLabel(new ImageIcon(getClass().getResource("/logo.png"))));

      

If you do not include a slash, the absolute name of the given resource will be:

modified_package_name/resource_name 

      



where modified_package_name

is the package name of the class object (i.e. getClass()

) with '/'

replaced with '.'

.

You can also use Class#getResourceAsStream()

that returns InputStream

, then converts to a byte array and passes it to the constructor ImageIcon

:

InputStream input = getClass().getResourceAsStream("/logo.png");
byte[] bytes = IOUtils.toByteArray(input);  // using commons-io library
panel.add(new JLabel(new ImageIcon(bytes)));

      

+3


source







All Articles