Fantastic! You have just finished your awesome Libgdx game and want to release it to Windows, GNU/Linux, and Mac users.
You could pack your project into a JAR file and distribute it, although it is far from optimal. First of all, your users will need to have a JRE; otherwise, they will not be able to run the game. Moreover, if their JRE is not compatible with the game's JAR archive, it may crash or cause all sorts of problems. Finally, you might not want your users to know that you are using Java.
The best way to go about distributing Libgdx games on desktop platforms is by packing your game with a JRE of your choice into a platform-specific binary. That way you will make sure it works without any surprises.
In this recipe, you will learn how to make a platform-specific binary of your Libgdx game.
We will be using Packr, an open source tool that does exactly what we need. Packr was created by Mario Zechner (author of Libgdx) and is maintained by the community. Go to its GitHub repository at https://github.com/libgdx/packr and follow the Download Packr link.
To pack your game with a JRE into a platform-specific binary package, follow these steps:
:jar
task with Gradle.java -jar packr.jar -platform windows -jdk "openjdk-1.7.0-u60-unofficial-windows-i586-image.zip" -executable mygame -appjar mygame.jar -mainclass "com/my/game/MainClass" -vmargs "-Xmx1G" -minimizejre "soft" -outdir out
Here is a description of all the parameters:
-platform
: This is either windows
, linux32
, linux64
, or mac
.-jdk
: This is the path to the zipped build of a compatible OpenJDK.-executable
: This is the name of your executable file.-appjar
: This is the path to your game's JAR package.-mainClass
: This is the fully qualified path of the main class of your desktop project using forward slashes to separate package names.-vmargs
: This is the list of arguments for the Java virtual machine separated by ;
. For example, -Xmx1G
gives a 1 GB RAM allowance for your game to run.-outdir
: This is the directory name where the generated files will be placed.-resources
: These are the additional resources, separated by ;
, you want packed besides your JAR file.-minimizejre
: This removes the unnecessary files from the JRE, you can either select "soft"
or "hard"
.Done! Now your application is completely self-contained; you will not have to worry about the JRE your users have.
Packr can be used programmatically to automatically package your projects. First of all, you will need to add the following dependency to your Gradle file:
compile "com.badlogicgames.packr:packr:1.2.0"
Create a Config
object, set the appropriate parameters, and instantiate Packr. Finally, call the pack()
method with the previously created config
object:
Config config = new Config(); config.platform = Platform.windows; config.jdk = "C:User/David/Development/openjdk-1.7.0-u60-unofficial-windows-i586-image.zip"; config.executable = "mygame"; config.jar = "mygame.jar"; config.mainClass = "com/my/game/MainClass"; config.vmArgs = Arrays.asList("-Xmx1G"); config.minimizeJre = new String[] {}; config.outDir = "out"; new Packr().pack(config)
3.139.239.41