jar file input == null while java app is working

448 views Asked by At

I'm currently coding a GUI which loads Images into it and creates collages of it.

My program is running fine while running it in eclipse as a Java Application but as soon as I compile it as a JAR File using Maven it gives me the error

F:\Dropbox\SWT4 Clean\1855992\target>java -jar 1855992-0.0.1-SNAPSHOT.jar
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:     input  == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at swt1.ub4.a1.GridGUI.createLabel(GridGUI.java:60)
    at swt1.ub4.a1.GridGUI.<init>(GridGUI.java:181)
    at swt1.ub4.a1.GridGUI.showGUI(GridGUI.java:243)
    at swt1.ub4.a1.GridGUI$4.run(GridGUI.java:255)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$300(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

This is the Error message my Commandline is giving me. I'm reading hte Image in using ImageIO.read:

try{
        firstImg = ImageIO.read(GridGUI.class.getClassLoader().getResourceAsStream("image.png"));
    } catch(IOException e) {
        e.printStackTrace();
    }

My project Setup:

I believe I have to add it into my JAR File manually using the pom.xml but how? If I add it to the src/main/resources folder then my JAR file is still not working.

Edit: pom.xml

<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>edu.kit.ipd.swt1</groupId>
<artifactId>jmjrst.pop-art-collage</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
    <groupId>edu.kit.ipd.swt1</groupId>
    <artifactId>uebungsparent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<repositories>
    <repository>
        <id>swt1</id>
        <name>swt1</name>
        <url>http://bob.ipd.kit.edu/nexus/content/repositories/swt1/</url>
    </repository>
</repositories>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.14</version>
            <dependencies>
                <dependency>
                    <groupId>com.puppycrawl.tools</groupId>
                    <artifactId>checkstyle</artifactId>
                    <version>6.4.1</version>
                </dependency>
            </dependencies>
            <configuration>
                <consoleOutput>true</consoleOutput>
                <configLocation>${basedir}/src/main/resources/checkstyle_swt1.xml</configLocation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptor>src/assembly/src.xml</descriptor>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>swt1.ub4.a1.GridGUI</mainClass>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencies>
    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>

2

There are 2 answers

2
mjrgr On

You should move image.png file to src/main/resources folder.

1
Hakim On

Lifepaths.class.getClass().getResourceAsStream(...) loads resources using system class loader, it obviously fails because it does not see your JARs.

You don't need to modify the pom.xml. Try this code:

ImageIO.read(GridGUI.class.getResourceAsStream("/image.png"));‌

instead of

ImageIO.read(GridGUI.class.getClassLoader().getResourceAsStream("/image.png"));‌