0

I have written a Java program, when it is run I add the name of every image file in the folder "items" into an ArrayList.

This is fine when I am running the application in eclipse. But as soon as I export to a runnable JAR file and run it, the images can't be found and returns a NullPointerException.

I've tried to use getClass().getResource() but to no avail.

This is what my Package Explorer looks like and This is what my exported JAR looks like

As you can see from the second image, all the images are taken out of the items folder and left in the root of the JAR.

Any help would be greatly appreciated!

J. Walsh
  • 43
  • 1
  • 4
  • So if i create a folder called items with all of the images in on the desktop it works, but id rather they were inside the JAR – J. Walsh Mar 10 '17 at 09:55

2 Answers2

0

try maven-assembly-plugin if you are using the maven build tool

How can I create an executable JAR with dependencies using Maven?

https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

Community
  • 1
  • 1
Chakradhar K
  • 501
  • 13
  • 40
0

Put this in your pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.maven.class... (Class path)</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>

</build>
Anirudh
  • 437
  • 2
  • 10