Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Customize NetBeans Platform Installer to Copy External Files During Installation

DZone's Guide to

Customize NetBeans Platform Installer to Copy External Files During Installation

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Installer software helps to copy application files to their expected locations. Well, not only to the application folder, but to any place on the system it is run on.

The NetBeans Platform installer infrastructure helps to package and distribute software to any supported machine. The job is routine but it gets a bit tricky when it comes to customization.

For instance, consider the situation that an external file needs to be copied somewhere before installation is completed.

To get started, open the project "helloworld" in the following NetBeans IDE installation folder:

<Netbeans installation folder>\harness\nbi\stub\ext\components\products

After resolving some dependencies (the required libraries reside inside the NetBeans installation dependencies) , open "org/mycompany/ConfigurationLogic.java".

Find the "install()" method. Paste the following code block just before the closing bracket:

File sourceFile = new File(“<path to your target file>");
File targetFile = new File(installLocation,"<wherever you want your target file to be copied>");
// installLocation is a variable containing the location which the application is installing in
try {
   FileUtils.copyFile(sourceFile, targetFile, true);
} catch (IOException ex) {
Logger.getLogger(ConfigurationLogic.class.getName()).log(Level.SEVERE, null, ex);
}
SystemUtils.getNativeUtils().addUninstallerJVM(new LauncherResource(false,targ));

To let the uninstaller finds and removes the external file later, find the "uninstall()" method right under the "install()" method. Paste the following code block just before "progress.setPercentage(Progress.COMPLETE)":

File external = new File(installLocation, "<path to your external file>");
if (external.exists()) {
   try {
   for (File file : FileUtils.listFiles(external .toList()) {
        FileUtils.deleteOnExit(file);
  }
FileUtils.deleteOnExit(installLocation);
   } catch (IOException e) {
}
}
It’s worth mentioning that the FileUtils class provides many useful methods that manipulate files and folders.

This solution described above follows the same path which Ernest used in his bundling the JRE article.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}