Java Electron is a Java project template for building a Java-based desktop app while using Javascript, HTML and CSS for the UI. It's like Electron but for Java.
If you have a Java website that you want to convert to a desktop app, Java Electron might be for you.
If you are proficient in Java (for backend) and JS/HTML/CSS (for frontend), Java Electron might be a good fit for you for building a desktop app.
This example provides the followings:
- A process for development with JS hot-reloading.
- A secure mechanism to communicate between the JS code and Java.
- A working process to codesign and notarize your app
- A working process to codesign and publish your app to TestFlight and Apple App Store
- A working CI that publishes a notarized app and a packaged app to TestFlight and Apple App Store. See .github.
You can download the app here: https://github.com/tanin47/java-electron/releases
Java Electron only works on Mac (Apple Silicon) for now. Other platforms are coming soon as I work toward launching the desktop version of Backdoor (Self-hostable single-jar database querying and editing tool for you and your team) on different platforms.
If you have questions or are stuck, please don't hesitate to open an issue. I'm always happy to help!
| Platform | Status |
|---|---|
| MacOS (Apple Silicon) | ✅ Supported |
| Windows | 🟡 In Progress |
| Linux | 🟡 In Progress |
| MacOS (Intel) | 🔜 Not Sure |
Run npm run hmr in one terminal and run ./gradlew run in another terminal.
This supports hot-reloading your JS code.
Run ./gradlew jpackage to build the DMG installer. Then, you can extract the DMG at ./build/jpackage.
You will need setup your bundle ID, certificate, and provisionprofile:
- Open
./build.gradle.ktsand modify the variables accordingly. - Replace
./src/mac-resources/provisionprofile/notarization/*.provisionprofilewith your provision profile, which you can get from https://developer.apple.com/account/resources/profiles/list - Run
./gradlew stapleto build, notarize, and staple the DMG.
You will need setup your bundle ID, certificate, and provisionprofile:
- Open
./build.gradle.ktsand modify the variables accordingly. - Replace
./src/mac-resources/provisionprofile/app_store/*.provisionprofilewith your provision profile, which you can get from https://developer.apple.com/account/resources/profiles/list - Run
./gradlew uploadPkgToAppStoreto upload the build to both TestFlight and App Store.
Java Electron uses webview to render the UI. The communication between the UI and Java goes through HTTP. Therefore, Java Electron consists of 2 main components:
- The web server (powered by Minum). Minum is chosen because it has no external dependencies and extremely small (350KB). It also powers Backdoor, which is the Java-based web app that I'm converting to a Desktop app. You can swap Minum with your fav web server framework!
- The web view (powered by webview) that points to the web server that serves JS/HTML/CSS assets. The current project uses Svelte + TailwindCSS + Webpack. You are welcome to swap it out for your fav Javascript framework.
The web server implements 2 security mechanisms to prevent MITM:
- The web view communicates through HTTPS using a self-signed certificate unique to each run. Since the communication is only for localhost, this is considered secure.
- The web server generates an API key unique to each run and passes it to the web view. The web view later passes the API key back through an HTTP request in order to authenticate itself.
With the above mechanisms, no other processes on your machine will be able to access the web server nor intercept the HTTP requests.
The motivation for using HTTP is to facilitate converting a web app that already uses HTTP. This would reduce a lot of code changes and branches while converting Backdoor, a self-hostable single-jar database querying and editing tool written in Java and Svelte. Using HTTP/AJAX/Fetch is also more familiar for me and many other people.
An alternative is to use the native "bridge" for communication. You can see an example in in the webview-java repo.
Java Electron uses a custom webview library (repo) that supports a self-signed certificate for localhost.
The webview library for Mac ARM has been built and put at ./src/main/resources/webview/libwebview.dylib. There is no need to rebuild the library.
