How to package your Java game this LD

Posted by
August 24th, 2012 5:01 am

Howdy, folks!

If you’re using Java to create your game, please take mercy on the people who want to play your game and consider using Launch4j or some other kind of packaging tool to package it. Launch4j makes it relatively easy to package together game code, game art, Java libraries, native libraries and a JVM.

The catch? Launch4j didn’t make immediate sense to me when I started using it, and it might not to you either. So here’s a tutorial on how to go from a JAR file and assets to a nice package. (Here’s an example of an end result for Windows users)

News Team, Assemble!

  1. Go get Launch4j.
  2. Make a new directory to assemble your package in. I’ve called mine “Recall”.
  3. Put your game code in a JAR file. In this example, my game’s JAR file is called “transient.jar”. Most development environments can do this for you. Eclipse calls this “Export”, I believe, and Netbeans builds a JAR as a part of normal building of your project.
  4. Put that JAR file in the root of the directory just created.
  5. Put all your game’s Java libraries inside a folder in the newly created directory called “lib”. It’s best to name it whatever it’s called inside your source project, but technically you can name it something else if you want.
  6. Hopefully, when you were developing your game, you had a folder in your project called “res” or “data” or something similar where you put all the game’s assets. Stick that folder in the root of the directory. Multiple folders introduce some clutter but should work as well.
  7. Put any native libraries your game uses, like the native components of the LWJGL, in the root of the directory.
  8. Put any other files that your game expects to be in the directory with it (logging configuration, splash screen, etc.) in the root of the directory.
  9. If you’re bundling a JVM, which I heartily recommend, copy that folder from elsewhere on your computer into place at the root of the directory as well.
visual overview of files in the packaging directory

Hot Pocket Drop It

  1. Open up Launch4j.
  2. On the Basic tab, select your game JAR file using the folder button to the right of the “*Jar” field.
  3. On Windows, Launch4j creates an EXE file. Click on the folder button to the right of the “*Output file” field and give your output a name, making sure it’s in the folder we’ve been working in all this time.
  4. If your game JAR file is awesome, you won’t have to do anything on the Classpath tab. However, I did. I had to enable the Custom Classpath option in order to specify a different main class than the one specified in my game JAR’s manifest, and also in order to verify it was looking in the right places for the Java libraries.
  5. On the JRE tab, specify the folder where we put the JRE from step 9 of the file assembly process. In my example, it would be “jre7”
  6. Save your Launch4j configuration file somewhere using the diskette button on the top left of the Launch4j window.
  7. Build your wrapper using the gear button on the top left of the Launch4j window.
  8. Test it using the play button.

Victory Shall Be Yours

You now have a functional packaged game that relies on no outside JVM, libraries, or other things that a player would have to set up!

I’ll be hanging out in the IRC channel for the working hours of the competition if you have any questions or problems using Launch4j – alternatively, I believe there’s a mailing list for Launch4j you can use. You can also drop me a line with the gmail at the spirulence if you like.

Good luck!


6 Responses to “How to package your Java game this LD”

  1. Canard Sauvage says:

    Or you can use Maven for your build and just use the maven-jar-plugin like this:

    org.apache.maven.plugins
    maven-jar-plugin
    2.3

    UTF-8
    true

    true
    true

    ${env.COMPUTERNAME}
    ${project.url}
    de.csw.ludum.dare.ld23.GameComponent

  2. Canard Sauvage says:

    narf, pasting xml isn’t working… But you can read about the usage here: http://maven.apache.org/plugins/maven-jar-plugin/

    • spirulence says:

      Thanks for the tip, Canard! If one prefers to build a JAR file versus an EXE wrapper, that’s an excellent choice.

      In a perfect world, I would probably combine these two approaches, putting as many resources in the JAR file as possible using some kind of build task, and then using Launch4j to bundle a JVM and provide a “one-click” experience. If I recall correctly, one can also run Launch4j as a build task, although I’ve never done it myself.

  3. My Milked Eek says:

    Thanks for the guide!

  4. heuermh says:

    Thanks for the write up for Launch4J.

    I wanted to add a few other resources for maven users:

    OS X Application Bundle Plugin
    http://mojo.codehaus.org/osxappbundle-maven-plugin/index.html

    Application Assembler Maven Plugin
    http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/

    IzPack Maven Plugin
    http://izpack.codehaus.org/izpack-maven-plugin/

    Another possibly more friendly option is to wrap your java application jar in a Processing sketch and take advantage of Processing’s Export as Application… option.

    Processing Application Export
    http://wiki.processing.org/w/Export_Info_and_Tips#Application_Export

    Cheers!

    • spirulence says:

      Thanks for the links, heurmh! And you’re welcome.

      Glad to have some OSX information here as well, as I’ve not very much experience with it or packaging Java apps for it.

      That Processing link is very interesting – will have to look further. :)

Leave a Reply

You must be logged in to post a comment.

[cache: storing page]