8th July 2023

How to pack JRE in Electron Builder

Build React Application

What is JRE?

The Java Runtime Environment (JRE) is a software package that includes multiple files and directories, which collectively provide the necessary components for running Java applications and applets.

Java Virtual Machine (JVM) The JVM is the most crucial component of the JRE. It is responsible for executing Java bytecode, which is the compiled form of Java source code. The JVM translates the bytecode into machine-specific instructions and allows Java applications to run on any platform that has a compatible JVM installed.

The JRE includes a set of standard class libraries, APIs (Application Programming Interfaces), and utilities. These libraries provide various functions and services to Java applications, such as file handling, networking, graphical user interfaces, and more. The JRE contains configuration files and resource files that help in setting up the Java environment correctly.

What is the Electron Builder?

Electron Builder is a popular open-source tool that simplifies the process of packaging and distributing Electron applications across multiple platforms. Electron Builder is an extension of the Electron framework, which allows developers to create cross-platform desktop applications using web technologies such as HTML, CSS, and JavaScript. Electron Builder can package your Electron application for various platforms, including Windows, macOS, and Linux. It handles the complexities of platform-specific packaging and creates installation files or executable packages for each target platform.

Why do we need JRE in our application

We may require an application that needs to interact with the user's Operating System. Java helps us to acheive that, therefore we need to develop an application that interacts with Java through some means, such as using Java bindings or Java-related libraries, then the JRE might be required when the application is running, not during the build process. In this case, you'll need to ensure that the target user's system has a compatible JRE installed. To clarify, during the build process of an Electron application, you don't typically include or reference a JRE, as it's not a requirement for creating the Electron package. The build process involves packaging the Electron app with Node.js, Electron runtime, Chromium, and your application code to create an executable or installer file suitable for distribution on different platforms (Windows, macOS, Linux).

Packing JRE to LINUX platform

To add the JRE dependency when getting the installers using electron builder for Linux is quite easy when compared to Windows. All we have to do is modify the package.json to get JRE installed along with the application. Copy the below package.json configuration for linux and get the build targetting Linux

                                
                                    
    "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "dev": "concurrently -k "cross-env BROWSER=none npm start" "npm:electron"",
    "electron": "wait-on tcp:3000 && electron .",
    "dist": "build",
    "build-installer": "electron-builder",
    "build-installer:linux": "electron-builder --linux deb"
    },
                                
                            

Notice that for the Linuc build we target it using deb

                                
                                    
    "build": {
    "appId": "application-id",
    "deb": {
      "depends": [
      "default-jre"
      ]
    }
    }
                                
                            

Then inside the deb configuration we can list the dependencies that we need to install, here since we need JRE we go with 'default-jre'

This is it, once the installer is installed in a Linux environment it will automattically install JRE to the system. Now lets look into getting JRE into Windows environment.

Packing JRE to WINDOWS platform

Getting JRE to our Windows platform is quite tedious when compared to Linux, there is no direct way to get JRE to our windows installer.

NSIS(Nullsoft Scriptable Install System)

NSIS is an open-source script-driven installation tool that allows developers to create Windows installers for their software applications. NSIS was originally developed by Nullsoft, the same company that created Winamp.

NSIS provides a simple and flexible scripting language that allows developers to define the installation process step-by-step, including file copying, registry modifications, creating shortcuts, and executing custom actions. The NSIS script files usually have the ".nsi" extension.

Now let's look into creating a script file to download and install JRE during the installation process

First, we need to obtain the JRE installer, which is typically an executable file (e.g., jre-8u301-windows-x64.exe). You can get this from the official Oracle website or any other reliable source.Create a custom NSIS script to include the JRE installer and set up the installation process. Save the script with the ".nsi" extension. Here's a basic example of how your NSIS script might look:

                                
                                    
    !include "MUI2.nsh" ; Include the modern UI for NSIS
    Name "MyElectronApp" ; Your application name
    Outfile "MyElectronAppInstaller.exe" ; Output installer file name
    !define JRE_INSTALLER "jre-8u301-windows-x64.exe" ; JRE installer filename;
    The default section
    Section; 
    Install your Electron app here (if you have additional files);
    Install JRE
    SetOutPath $TEMP ; Set the temporary directory for the JRE installer
    File '{JRE_INSTALLER}' ; Include the JRE installer in the installer package
    ExecWait "$TEMP${JRE_INSTALLER}" ; Execute the JRE installer;
    Add any other steps you need during installation here
    SectionEnd;
    Uninstaller section
    Section "Uninstall";
    Uninstall your Electron app here (if necessary);
    Uninstall JRE
    Delete "$INSTDIR\path\to\uninstall\jre\files" ; 
    Remove any JRE-related files installed with your app; 
    Add any other steps you need during uninstallation here
    SectionEnd;
    The following code will set up the installer appearance;
    (customize as per your requirements)
    !define MUI_ABORTWARNING
    !insertmacro MUI_PAGE_WELCOME
    !insertmacro MUI_PAGE_DIRECTORY
    !insertmacro MUI_PAGE_INSTFILES
    !insertmacro MUI_UNPAGE_CONFIRM
    !insertmacro MUI_UNPAGE_INSTFILES
    !insertmacro MUI_LANGUAGE "English"
                                
                            

Conclusion

Therefore by using NSIS we can get JRE into the client's machine whether he has JRE previously installed or not thus helping us running our application which needs JRE to execute

Let's develop your ideas into reality