James Williams
LinkedInMastodonGithub

Creating Java Executables From PlayN

Tags: Gaming

While persuing the PlayN group list, a question came up about packaging a Java target for distribution. Here's the result of my serveral hours of playing around. For this task we'll be using some additional Maven plugins, specifically JarJar Links.

JarJarLinks is a library that allows you to package a Java app and all its dependencies into a single file. Your users don't have to worry about where the dependencies are located and can just double-click the jar file to run it. JarJar also has some advanced features to omit or rename certain classpaths. We won't be delving into those today.

To build an uber jar, you need to add a couple of plugins to the pom.xml file of the Java backend. These should be added in the executions section inside plugins. The first one is the JarJar package and the settings below state that you want it to attach to the package phase and run without any custom rules.

<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>jarjar-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>jarjar</goal>
            </goals>
            <configuration>
                <rules />
            </configuration>
        </execution>
    </executions>
</plugin>

Running mvn package now will sure enough create an uber jar but trying to run it gives a Main-Class not found (or similar) error. To fix that, we need to tell the jar phase which class we want to run when the jar is executed. The class name here should match the full path of your Java backend class name.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.html5gamebook.demo.java.JarJarDemoJava</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

If you have previously run mvn package before adding the last bit, delete the java/target directory and re-run it. You should be all set.