This article describes how you can make a Windows executable or a Mac App out of a Java app. In my example, I created a Java Swing based app, and made both a EXE and .app
Of note, the executable size is going to be pretty large once installed, as it includes a large piece of the Java runtime. My 29KB JAR was turned into 170MB once deployed on either system. However, the EXE we are going go create an installer, and it compresses to 40MB. On the Mac we are just going zip it and it gets the size down to 61MB.
On a mac, you have the option of making an application bundle (a folder with the .app extension that contains all the app resources), a DMG, or a package that people can install. We will be making an application bundle here.
Create an icon using your favorite image editor. I use Pixelmator and export to a PNG. I've been using a 128x128 icon, so resize it as well. From there you will make an .icns file used for your application icon.
mkdir YourApp.iconset cp youricon.png YourApp.iconset/icon_128x128.png iconutil --convert icns YourApp.iconset ls *.icns
With the JAR and icon already build, the next step is to make the app bundle. You will get a warning Did not find a key matching 'Developer ID Application: ' using this technique. It's likely because I haven't setup my Apple developer certs to be used.
$ javapackager -deploy \ -title "YourApp" \ -name "YourApp" \ -appclass com.axorion.YourApp.YourApp \ -native image \ -Bicon=YourApp.icns \ -outdir dist \ -outfile YourApp.app \ -srcfiles target/YourApp.jar No base JDK. Package will use system JRE. Creating app bundle: /Users/lee/workspace/java/prettycsv/dist/bundles/PrettyCSV.app Did not find a key matching 'Developer ID Application: '
Once complete you will find your app in dist/bundles.
Very similar process, but we will be outputting an EXE installer. Cool thing about it is that as soon as in installs, the app runs. The user will be able to uninstall your app using the standard Windows Uninstall Apps & features in the control panel. The app will install to C:\Users\lee\AppData\Local\YourApp. That is also where all the Java DLLs are located. I'm sure there is a setting to change that location, but I haven't taken the time to find it yet.
I didn't have any tools for building a Windows ico file, so I used an online tool. Create your icon and export it as a png. Upload to the online tool and convert to an ico file. Download and toss it into your images folder.
You will need to make sure that Inno Setup is in your path as javapackager needs to have iscc.exe in your path.
C:> javapackager -deploy -title "YourApp" -name "YourApp" -appclass com.YourApp -native exe -Bicon=images/YourApp.ico -outdir dist -outfile YourApp.app -srcfiles target/YourApp-1.0.jar No base JDK. Package will use system JRE. Installer (.exe) saved to: dist\bundles
And that's it. If you found this helpful, hit that like button below. Thanks!
Copyright © 2020, Lee Patterson