Installing the JRE via an Application's Installer via Windows

In contrast to Ernest's excellent instructions, 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. Hence, these instructions are more limited, but also simpler, focusing purely on the Windows scenario.
  1. Go to your JDK installation and copy the JRE somewhere else, separately (e.g, "C:\\Program Files\\Java\\jre"), so you don't end up messing up the JDK you're actually using to run, for example, NetBeans IDE.

  2. Go to C:\\Program Files\\Java\\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".

  3. 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".

  4. Put unzipsfx.exe (which you can get from the NetBeans sources, in "nbi/infra/build/jvm" or from somewhere online) into the same folder as where the jre.zip is found. Run this:
    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. 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.

  5. 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:\\Program Files\\Java\\jre.exe"/>
    </create-bundle>

  6. 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));

    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.

  7. In your application, e.g., in "nbproject" of your application, create a ".conf" file with, among other things, this entry:
    jdkhome="jre"

    And register the conf file in the application's "project.properties" file like this:

    app.conf=nbproject/my.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.

Comments:

Geertjan,
Thank you for this, it is excellent. Can the same technique be used to add other executable programs in another directory?
If so, how would one reference the directory within the app once it is installed?
Thanks !

Posted by Sony C on April 28, 2011 at 11:50 AM PDT #

Great. Notice that you also need to add the default_userdir in the application conf file with a line like : default_userdir="${HOME}/.myapp" However I encountered troubles with the generated app because I lost my custom splash screen and my application name which is set to the default "Netbeans Platform 7.0" name. I have to check if it's related to that (it works fine when running it inside Netbeans)

Posted by David on May 12, 2011 at 04:40 PM PDT #

Hi Geertjan,

I'd like to push the question raised by Sony C. I'd also like to bundle an external application (a xampp distribution) within the installation dir of my netbeans plattforma application.

Unfortunatly I didn't find any information about how to do that but somehow it should be possible as Netbeans IDE self ships a tomcat server with it.

I hope you can point me to a document describing how to accomplish this.

Greetings,
SmilingJ

Posted by guest on July 10, 2011 at 03:30 AM PDT #

SmilingJ, just follow the same steps above and replace JRE with your own app.

Posted by Geertjan on July 10, 2011 at 04:57 AM PDT #

Hi Geertjan,
I have followed above steps and I am able generate jre folder under my application after installation. But unfortunately the application is not starting as well as not giving any error.
Also I found that the size of the application becomes too big. Is it correct.
Let me know
Thanks!

Posted by guest on June 13, 2012 at 06:54 AM PDT #

Hi Geertjan,
We planning to make a commercial application based on netbeans platform. Our end user machines (mostly windows and linux) will not have any jre pre-installed on it. Using NBI and the approach you and Ernest have mentioned we can bundle JRE to be private jre's for our application. Are there any licensing restrictions from oracle which will avoid us from embedding JREs within our application? I read through licensing part of JRE at oracle and am sort of confused. It would be great if you can advise us if its legal to bundle JRE with our commercial application.

Thanks a lot for your guidance.

Posted by Bhan on July 25, 2012 at 07:21 AM PDT #

No licensing restrictions. Would be great if you'd send me a screenshot and description of your commercial application to geertjan dot wielenga at oracle dot com!

Posted by Geertjan on July 25, 2012 at 09:37 AM PDT #

I am trying to follow this using Windows7 and Netbeans 7.2.1.

When executing the pack200 in the CMD window, I get an exception

"Exception in thread "mai" java.io.FileNotFoundException: rt.jar(the system cannot find the file specified: rt.jar

at java.util.zip.ZipFile.open(Native Method).
.....
at com.sun.java.util.jar.pack.Driver.main(driver.java 233>

I am new to java and netbeans, but a search of my disks reveals that no java.util...

Where do I get it and how should I ann it to my java or netbeans?

Is the CMD window the right place to be trying this?

Posted by Ann Maybury on December 09, 2012 at 10:49 AM PST #

I solved my last problem. User error. Please ignore.

However, I have spent a lot of time on the web trying to find unzipsfx for windows. No luck. Your article refrences building it, but I have had no luck finding the source.

Can you tell me where to get it?

Ann Maybury

Posted by Ann Maybury on December 09, 2012 at 02:31 PM PST #

Where does the article tell you that you need the unzipsfx source? You don't.

Posted by Geertjan on December 10, 2012 at 02:09 AM PST #

PERFECT

Posted by guest on December 16, 2012 at 07:57 AM PST #

Thanks, I am able to bundle jdk and third party applications.

Is it possible to bundle two jres (32 and 64 bits)?
Can the installer resolve which jre to include ?

Asutosh Gopinath

Posted by guest on February 07, 2013 at 02:23 AM PST #

i got my answer here:

http://netbeans.dzone.com/including-jre-in-nbi

asutosh gopinath

Posted by guest on February 07, 2013 at 08:18 PM PST #

Hi Geertjan,

I have tried all the steps above (as well as referred to the wiki). However, when I run the generated installer, it fails saying that the JVM is not compatible. The verbose output has a line "unpack200 not found" just after the entire bundled JRE has been successfully extracted.

Can you tell me where I might be going wrong here?

Thanks!
Rashmi

PS: I would like to mention that your blog is absolutely great! I have referred to it a number of times and mostly my issues have been solved. Thanks for maintaining it!!

Posted by guest on August 22, 2013 at 10:19 AM PDT #

I found the issue myself!! The log for the installer is quite unhelpful when it comes to errors - its tough to figure out what is the exact command creating an issue. Turns out I had bundled the JRE incorrectly - with a parent folder jre containing the bin and lib.

Rashmi

Posted by guest on August 26, 2013 at 05:13 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today