http://blogs.oracle.com/geertjan/entry/installing_the_jre_via_an

——————————————————————————————————————————————————————————————————

 

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.

——————————————————————————————————————————————————————————————————

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

 

The NetBeans Platform provides a very nicely integrated installer infrastructure (NBI) which alllows for creating native installers for various platforms (Linux and Windows, in particular). This does, in a cross-platform way, what various other installers do (NSIS, IzPack, etc), but with the advantage of being NetBeans Platform aware and completely configurable.

It is not immediately clear how to bundle a JVM because using the default "Package As | Installers" option in NetBeans IDE 7.0 will build installers that are dependent on an already installed JVM on the system. In this guide, with help from Geertjan and Dmitry, we aim to configure the build process so that:

    • Native installers will be built for (at least) Windows and Linux.

    • These native installers will have a bundled JVM, and thus not require the system they are installed on to have a pre-installed JVM.

  • Ensure that the installed NetBeans Platform application uses a private JRE (in fact, the same one that was bundled before) which is therefore not visible to other applications in the system.

Getting a nice installer cycle with private JRE under Ubuntu Linux (Windows, very similar), to build both Windows and Linux installers from within Ubuntu.

Create JRE's to bundle

# Creating a directory where I will keep the JREs to bundle in various installers,
mkdir ~/bundled_jres
cd ~/bundled_jres

# 1. Set up Linux JRE
cp -r /some/path/to/linux/jre linux_jre (if using Windows, use xcopy /E or Windows Explorer)
cd linux_jre
# pack200 it
pack200 -J-Xmx1024m lib/rt.jar.pack.gz lib/rt.jar
# zip it
zip -9 -r -y ../linux_jvm.zip .
cd ..
# get unzipsfx for Linux
cp /usr/bin/unzipsfx unzipsfx.bin
# concatenate sfx header with zip payload, this results in a self-extracting jvm executable
cat unzipsfx.bin linux_jvm.zip > linux_jvm.bin (if using Windows, use copy /B unzipsfx.bin + linux_jvm.zip linux_jvm.bin)

# 2. Set up Windows JRE
cp -r /some/path/to/windows/jre windows_jre (if using Windows, use xcopy /E or Windows Explorer)
cd windows_jre
# pack200 it
pack200 -J-Xmx1024m lib/rt.jar.pack.gz lib/rt.jar
# zip it
zip -9 -r -y ../windows_jvm.zip .
cd ..
# get unzipsfx.exe for Windows, can find in ftp://ftp.info-zip.org/pub/infozip/win32/unz600xn.exe
cp /path/to/unzipsfx.exe unzipsfx.exe
# concatenate sfx header with zip payload, this results in a self-extracting jvm executable (Windows)
cat unzipsfx.exe windows_jvm.zip > windows_jvm.exe (for Windows, use copy /B as above)

At this point, we have a set of JVMs (not necessarily only Windows and Linux) which we can re-use for perpetuity, or update when significant new releases of the JRE becomes available.

Set up build.xml for NBI

Now, open <netbeans-install-dir>/harness/nbi/stub/build.xml :

Search for "-generate-bundles" target and replace the <create-bundle> call by the following set of conditional calls (changing /home/ernest to rather be your home dir of choice):

 

<!-- Linux installer -->
<if property="platform" value="linux">
    <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="/home/ernest/bundled_jres/linux_jvm.bin"/>
    </create-bundle>
</if>
 
<!-- Solaris installer -->
<if property="platform" value="solaris">
    <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="/home/ernest/bundled_jres/linux_jvm.bin"/>
    </create-bundle>
</if>
 
<!-- Windows installer -->
<if property="platform" value="windows">
    <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="/home/ernest/bundled_jres/windows_jvm.exe"/>
    </create-bundle>
</if>
 
<!-- Mac installer -->
<if property="platform" value="macosx">
    <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"/>
    </create-bundle>
</if>

 

(In the above, one could have multiple JVM's for multiple platforms, and just hook them up in the correct way as here. For example, in the Mac installer above we don't bundle any JVM.)

After doing this, generated installers should run off their bundled JVM's, but the application itself will not use this JVM, and search for a system JVM. To rather let it depend on this private JRE, continue …

Letting the application depend on the private JVM

Now, we edit ConfigurationLogic.java in <netbeans-install-dir>/harness/nbi/stub/ext/components/products/helloworld/src/org/mycompany/ConfigurationLogic.java to (a) extract the previously bundled JRE to a subdirectory called “jre” upon installation and (b) ensure it is also removed when the application gets uninstalled.

Add import statement:

 

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

 

At the end of install(), add:

 

 

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 in ConfigurationLogic.java
catch (IOException e) {
    throw new InstallationException("Cannot copy JRE",e);
}
         
// to add uninstaller logic:
SystemUtils.getNativeUtils().addUninstallerJVM(new LauncherResource(false, target));

 

and at the end of uninstall(), but just before progress.setPercentage(Progress.COMPLETE);, add

 

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
    }
}

 


Hook up a custom application configuration file, following Geertjan's http://blogs.sun.com/geertjan/entry/support_for_custom_configuration_files approach, but ensure that my.conf contains the line
jdkhome="jre"

 

 

which will be a relative path to the JRE that was installed by ConfigurationLogic's install() method.

Finally, to create our set of installers for various platforms and their respective bundled JRE's, right-click on your suite application, select "Package As", then choose "Installers". To create more compact installers (compressing .jar files using pack200), you can enable this by right-clicking, selecting Properties, select "Installer" and check "Use pack200 compression" - this has the effect that building the installers takes quite a bit longer due to the pack200 compression, but your users will be quite happy to have significantly smaller installers to download.

Many thanks to Geertjan Wielenga and Dmitry Lipin (the NetBeans Installer guru) without whom finding all the necessary ingredients would not have been possible!

 

 ——————————————————————————————————————————————————————————————————————————————————————————————

———————————————————————————————————————————————————————————————————————————————————————————————

需增加两个import:

import javax.swing.JOptionPane;

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

 @see https://dzone.com/articles/including-jre-in-nbi

 

 

 

 

posted on 2011-09-25 08:50  网络大豆  阅读(708)  评论(0编辑  收藏  举报