4

I need to be able to create an executable jar for my project where everything is packaged in the one jar. I think that the Maven shade build plugin is the best way to do this however I'm experiencing some problems loading my resources when running the created uber jar version.

To explain this a little futher, when trying to load my resources I experience a NullPointerException because the resource cannot be located. On inspection inside the created jar the resources are located in the root so I cannot figure out why it cannot locate them.

To clarify my problem I've created this small example, which prints the contents of a file. The structure is as follows: Debug |-- pom.xml `-- src |-- main | `-- java | `-- Main.java |-- resources `- tmp.txt

And the actual content of the files are:

Main.java:

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

    public static void main(String[] args) throws IOException, URISyntaxException {
        URL url = Thread.currentThread().getContextClassLoader().getResource("./tmp.txt");
        Path path = Paths.get(url.toURI());
        String str = new String(Files.readAllBytes(path));
        System.out.println(str);
    }
}

tmp.txt:

Hello World!

pom.xml:

<?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>debug</groupId>
    <artifactId>debug</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-cp</argument>
                        <classpath/>
                        <argument>Main</argument>
                    </arguments>
                </configuration>
            </plugin>

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

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

If you run this on the command line via mvn exec:exec everything works fine and the expected result of Hello World! is output.

However running mvn package followed by java -jar target/debug-1.0-SNAPSHOT.jar leads to the following error:

Exception in thread "main" java.lang.NullPointerException
    at Main.main(Main.java:12)

I'd be very grateful if someone could point me in the right direction or tell me how to change my small example to get this to work!

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sarah Tattersall
  • 1,275
  • 2
  • 21
  • 32

0 Answers0