First off: This is an Impatient Guide and it will be short on explanation and possibly accuracy
Second off: This method is a first attempt and is probably fraught with errors and inefficiencies, but it worked for me.
Let's get started!
Say you have a LibGDX project and you want to package it up for redistribution, but you don't want to give out a jar and would like it to be wrapped in a nice executable (.exe) just like any ordinary Windows binary. In addition, obfuscating the code to make reverse engineering more difficult might be something you want too.
Creating the JAR files:
- Export a jar file with the Eclipse Export Wizard. I did this for my desktop version, by right clicking on the project "myapp-desktop" -> Export -> Java -> Runnable Jar File.
- Plug in the usual stuff into the wizard, but choose "Copy required libraries into a sub-folder next to the generated JAR." Otherwise, ProGuard will make your life a nightmare when you try to obfuscate (e.g. "Warning: org.lwjgl.input.Controllers: can't find referenced class net.java.games.input.ControllerEnvironment").
- Hit "finish" and the wizard will generate your shiny new .jar file.
- Do 1 through 3 again, but in step 2 choose "Package required libraries into generated JAR." The jarinjarloader files and the MANIFEST.MF from this second generated JAR will be used later on.
Obfuscate your jar with ProGuard:
- Run the GUI version of ProGuard: java -jar proguardgui.jar
- In the Input/Output tab, click "Add input..." and enter the first generated JAR path.
- Click "Add output..." and enter your output JAR... say (e.g. output.jar).
- Click "Add..." in the Add libraries section and add the library export directory generated with the first JAR. If your export JAR name was foo.jar, the export dependencies should be in foo_lib.
- In the Obfuscation tab uncheck "Use mixed-case class names."
- Keep the default settings for now (you can tweak them later once you have this working).
- Start processing the JAR. In the Process tab click Process!
- If ProGuard finishes it will produce a nice obfuscated/shrunk JAR, output.jar.
Combine library dependencies into obfuscated JAR
- The output.jar is not runnable as is. It needs to know how to find the library dependencies. You can pack the dependencies into the JAR with 7-zip. So, open output.jar in 7-zip.
- Add/drag the contents of foo_lib (from step 2 in the first section) into the root level of the JAR (e.g. they should be in the same directory META-INF is in).
- Extract the other JAR from the first section (see step 4) into a temp directory (e.g. temp).
- In output.jar, clobber the file "META-INF/MANIFEST.MF" with the file from "temp/META-INF/MANIFEST.MF".
- In output.jar, add the directory "org/eclipse" from "temp/org/eclipse".
- Confirm the JAR is runnable: java -jar output.jar
Wrap your JAR in a Windows executable
- Use Launch4j to wrap your JAR. Open the Launch4j GUI.
- Add your output file: out.exe
- Add your input JAR file: output.jar
- In the JRE tab, set the Min JRE version (e.g. 1.6.0)
- Click the gear icon to build the wrapper.
- Confirm the binary works. High Fives all Around!