Learning The "J"

Home » Netbeans Platform » Bundled JRE in Netbeans Platform Application Installer

Bundled JRE in Netbeans Platform Application Installer

This post is purely based on the guidelines provided by Geertjan Wielenga https://blogs.oracle.com/geertjan/entry/installing_the_jre_via_an

These below assume that (1) you are creating your installer on Windows and (2) you’d like your installer to be used on Windows only.

  • Go to your JDK installation and copy the JRE somewhere else, separately (e.g, “C:\\jre”), so you don’t end up messing up the JDK you’re actually using to run, for example, NetBeans IDE.

jreactual

jrebackup

  • Go to C:\jre\\lib and run:

pack200 -J-Xmx1024m rt.jar.pack.gz rt.jar

Now go into C:\\Program Files\\Java\\jre\\lib and delete the “rt.jar”.

Note:       As pack200 is actually in bin folder so you probably need to mention

c:\jre\bin\pack200  -J-Xmx1024m rt.jar.pack.gz rt.jar

  • Go to C:\\Program Files\\Java\\jre and select the “bin” folder and the “lib” folder. Right-click and ZIP the selected folders. You’ll end up with “jre.zip”.

zipjre

Put unzipsfx.exe (which you can get from the NetBeans sources, in “nbi/infra/build/jvm” or from somewhere online http://code.metager.de/source/xref/netbeans/nbi/infra/build/jvm/tools/unzipsfx/) into the same folder as where the jre.zip is found. Run this:

unzipsfx

copy /B unzipsfx.exe + jre.zip jre.exe

The result of the above is that you have “jre.exe”, a self-extractable zip archive, which you can run and then you’ll find the “jre” installed on your system.

jreexzip

 

 

We now need to bundle this jre.exe into the installer, via the Ant script that configures the installer, as outlined in the next step.

  • Replace “create-bundle” in build.xml in the IDE’s “harness/nbi/stub/build.xml” with the following:

<create-bundle root="${output.dir}/registry-temp" platform="${platform}"        target="${bundles.release.dir}/${bundle.files.prefix}-${platform}.${bundle.extention}">
    <component uid="${main.product.uid}" version="1.0.0.0.0"/>
<property name="nbi.bundled.jvm.file" value="C:\\custom\\jre\\jre.exe"/>
</create-bundle>


buil

  • Put this right at the end of “install()” in ConfigurationLogic.java, which is within “harness/nbi/stub/ext/components/products/helloworld/src/org/mycompany”:
File javaHome = new File(System.getProperty('java.home'));
File target = new File(installLocation,jre);
 try {
  FileUtils.copyFile(javaHome, target, true); //FileUtils is one of the NBI core classes, already imported
 } catch (IOException e) {
  JOptionPane.showMessageDialog(null,'Cannot copy JRE');
 }
// to add uninstaller logic:
  SystemUtils.getNativeUtils().addUninstallerJVM(new LauncherResource(false, target));
  • Dont forget to add import statement in ConfigurationLogic.java for LauncherResource class
 import org.netbeans.installer.utils.system.launchers.LauncherResource;
  • And, in the “uninstall()” method, add this right before the last statement:
File jre = new File(installLocation,jre);
if (jre.exists()) {
 try {
  for (File file : FileUtils.listFiles(jre).toList()) {
   FileUtils.deleteOnExit(file);
  }
  FileUtils.deleteOnExit(installLocation);
} 
catch (IOException e) {
 //ignore
}

}

You have now specified how and when the “jre” will be installed by the installer wizard.

import org.netbeans.installer.utils.system.launchers.LauncherResource;

  • In your application, e.g., in “nbproject” of your application, create a “.conf” file with following entries:

conf

default_userdir=”C:\\SKMCONFIG”
default_cachedir=”C:\\SKMCONFIGCACHE”
default_options=”-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-ea”
jdkhome=”jre”

  • And register the conf file in the application’s “project.properties” file like this:

prjprop

app.conf=nbproject/SKMDataReciever.conf

Now you have specified that the application will use your jre.

When you right-click the application and choose “Package as | Installers”, an executable file will be created. When you run the executable, an installer starts, the JRE is unbundled to a temp folder, the installer uses that JRE, and a copy of the jre is made to a folder named “jre” within the root folder of your application, where the “jdkhome” setting in the .conf file finds it. That JRE, i.e., a private JRE within your application’s root folder, is then used to start up the application.

In this way you can distribute your application to users who do not have the JRE installed on their system.

Advertisements

2 Comments

  1. Learning The "J" says:

    Please make sure that you pass brandingname in the default_options. Otherwise Custom Splash screen will not appear

    default_options=”–branding ${branding.token} -J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-ea”

    Thanks to Geertjan for this information

  2. Asad Sohail says:

    Hi Sir, Its me Asad Sohail, I am finally getting help from your blog to redo all the steps. Thanks to you 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Start here

%d bloggers like this: