This will show you how to package all your java class files into a single executable jar file and how to make it runnable when you double click it. In our project we might need to load external resources like images from subfolders and other .jar libraries like slick.jar and lwjgl.jar. I used the eclipse IDE to develop my project. 1. Once you’ve got it working in eclipse, right click the root folder of your project in the Package Explorer and choose Export, then Java > Runnable JAR file. 2. Choose the option that says “extract required libraries into generated JAR”, choose a directory to export and click finish, accept the warning message if you get any. I’ll be using the name jarFile.jar in this example. 3. Now we need to make sure than any other subfolders folders in your project, like /images or /sounds or anything else you had are included. Copy all of these subfolders into the same folder as the .jar file you made. Ensure that all the external libraries files (.dll and .so, etc) that you need are also included. Just as you copied the subfolders, copy all these library files into the same directory as the .jar file. 4. Double click your .jar file now, it should be runnable as long as it can find all the files it needs to run. Run it in the command line to see if there are any errors:
java -jar jarFile.jar
Making A Runnable EXE File5. If you want to make an exe file for Windows to run your .jar, you can use a handy free tool called exej. Note that if you have the Java Runtime installed in Windows, you can just double click a .jar to run it, however if you want to add extra runtime arguments like changing the classpath or changing the size of the memory heap available for your program, exej makes that possible. 6. Download exej and extract the files in the zip to another folder than the jar file. 7. Inside the folder with exej.exe, there will be a text file called config.txt. Edit the file and change the command line arguments that you want, make sure that you have ”-jar jarFile.jar” at the end so that the exe knows what jar file to execute with the arguments. In this example, I increase the maximum heap size to 128MB. You can add any of the regular java arguments as you would in the command line.
commandline= -Xms32m -Xmx128m -jar jarFile.jarAlso make sure that you rename jarFile.jar to your .jar filename. 8. Now run make.bat, which is also in the exej folder, to make your exe files with config.txt. It will make two, one that runs java (with command line window) and javaw (without command line window). 9. Finally, all is done, copy these exe files into the same folder you put your .jar file, the subdirectories, and all the other .dll and .so library files you had. Run either of the exe files to test your program. You can, as usual, just run the .jar file by double clicking it, but this won’t have the arguments we made with exej, you’ll need to run it through the command line as usual with something like:
java -Xms32m -Xmx128m -jar jarFile.jarAnyway, if you’ve got any problems, leave a comment below.