0

I am trying to execute a Java application using it's jar file. The Swing based application has some resources under res folder, where I keep my images to show them in the JFrame. Here is a screenshot of the structure of the project, you can see that res folder is located both under src folder and under classes folder:

I am using Netbeans, and the project is a Maven project, here is the pom file, which is used to generate the jar and add some dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.solent</groupId>
    <artifactId>ReportGenDist</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>maven</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>1.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>AsposeJavaAPI</id>
            <name>Aspose Java API</name>
            <url>http://repository.aspose.com/repo/</url>
        </repository> 
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>


    <build>
        <finalName>ReportGen</finalName>
        <plugins>
            <!-- download source code in Eclipse, best practice -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.solent.main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>                         
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I am loading the first displayed image in the frame with the following line:

deleteBtn1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/delete.png")));

When I run the project within NetBean's IDE it works just fine, however, when I'm trying to run the jar file from the command prompt, I get the following exception:

Exception in thread "main" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        at com.solent.gui.MainGUI.initComponents(MainGUI.java:145)
        at com.solent.gui.MainGUI.<init>(MainGUI.java:51)
        at com.solent.gui.LogInGUI.<init>(LogInGUI.java:17)
        at com.solent.main.Main.main(Main.java:23)

Can anyone please help me solve this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I doubt that this: `maven-jar-plugin` is a correct dependeny? If this meant to be the apache maven jar plugin then it's simply wrong cause a plugin should never being used as a dependency. Furthermore you should remove maven-eclipse-plugin cause it's retired for a longer time..IDE's can handle that better in the meantime..(apart based on the screen shot you are using Netbeans and not eclipse).. – khmarbaise Mar 04 '20 at 19:19
  • Thanks for the comment, @khmarbaise already deleted it and jar is still being correctly generated. However, I still did not solve my issue with that NullPointer – Diego Echenique Mar 04 '20 at 19:34
  • This might be a classpath problem when the application is packaged as a jar. Did you try this: https://stackoverflow.com/a/20389418/3266467 – Sebastian Mar 05 '20 at 12:01
  • Thanks for the comment @Sebastian I have tried it, but imageIcon does not have a constructor for an InputStream or an InputStreamReader – Diego Echenique Mar 05 '20 at 18:09

0 Answers0