JavaUtils: Detecting and Installing Java from AIR


Adobe AIR is a great way to bring the sexiness of Flash to the desktop. However, sometimes you need more low-level power for things like connecting to peripherals. For this reason, AIR applications are sometimes paired with Java applications to accomplish such tasks. But first, the user’s system must have an adequate Java Runtime Environment (JRE) installed in order for the Java application to run in the first place.

Before we get too far, be aware that these JavaUtils are currently for Windows only. Macs have their own version of the JRE that’s deployed through the Software Update feature so the process of detecting and installing the JRE is very different and is left for another day.

The Short Story

To get started with the utilities, first download the source by checking out the git repository or installing the sample application. If you choose to install the sample application, run the app and right-click on the stage to view the source.

The utilities include two executables:

  1. whereis.exe – This is used internally by the tools. See the juicy details below if you care to learn more about this.
  2. jre-blah-blah-blah.exe – This is the Windows JRE online installer found here: This is the installer that is used to install a JRE of your choice if the user’s system does not have an adequate JRE already installed. Because it’s the “online” installer, it’s lightweight and lets you avoid bundling an entire JRE with your AIR app.

There are two main utility classes:

  1. JavaValidationUtil – This is used to determine if the user has an adequate JRE installed. Call validate() and it will dispatch an event letting you know if the user has a JRE installed and if the version is sufficient.
  2. JavaInstallUtil – This is used to install the JRE if needed. Call install() and it will dispatch an event letting you know when the installation is complete.

The Juicy Details

Let’s dig into the details of how this works. Support for running other executables as described below comes from the new NativeProcess in AIR 2.0.

Validation. The first chore is to validate whether the user has a JRE installed and, if so, whether the JRE is sufficient for the application’s needs. The developer provides the validation utility with a minimum acceptable version broken into four segments: major, minor, revision, and update.

Now we need to find the version of the installed JRE to compare. But first we need to find out where the JRE is installed if one exists. On Windows there’s an environment variable called PATH which contains several directories that should be searched to find an application when a command is executed in the command shell. When you open up cmd, for example, you can type “notepad” and hit enter. The fact that it’s able to open notepad isn’t magic. Windows simply searches the PATH directories for notepad.exe and then executes it.

We need to do the same thing to find the java executable (java.exe). On *nix operating systems this is pretty easy. There’s a simple tool called whereis that comes with the operating system that searches the paths to find a given executable. On Windows, not so much. Luckily, our friends over at Synesis have released a similar tool for Windows that allows us to do the same thing. We bundle whereis.exe with our AIR application.

We execute whereis.exe, passing in name of the executable we’d like to find (java.exe) as an argument. If we don’t find java.exe, the user doesn’t have a JRE installed–at least not in the PATH paths. And if a JRE does exist that’s not in those paths it could take a long time to find and we probably don’t want to mess with it anyway. If java.exe is found, we then execute it passing in the -version argument. Java will then output the version in a specified format that we can then parse. After we parse the version, we compare with the minimum acceptable version and dispatch appropriate events.

Installation. If no JRE is installed or the one installed isn’t adequate, the application would then generally notify the user they need a newer JRE and ask if they’d like to proceed with the installation.

If the user chooses to proceed, it’s time to execute the installer. But there’s a catch. In Windows, installer applications require “elevated privileges” before executing which means Windows nags the user asking for permission to install the JRE. If I were to double-click the installer when it was on the desktop, it would appropriately nag. However, if I execute the installer using AIR’s NativeProcess, nothing happens. No error, no output, no nagging, no installing, nothing. Is this a bug in AIR? Eight ball says signs point to yes, but who knows, maybe Windows is to blame.

In my search for a workaround I found that by executing the installer from the command shell, Windows again appropriately nagged. So then I tried executing cmd from AIR and having cmd execute the JRE installer by passing the path of the installer into cmd as an argument. What do you know–Windows nagged appropriately and off goes the installation as planned. When presenting this problem on the Adobe AIR forums, not receiving a response, then proposing the workaround, a member of the AIR engineering team seemed to think it was a good trick–nay, a great trick–so I guess that’s just the way we have to do things for now.

So first we use our trusty whereis.exe to find the location to cmd.exe. Then we use cmd.exe to execute our Java installer by passing the installer path into cmd as an argument. Windows prompts the user for permission and after permission is granted the installation proceeds as expected. After the installation is complete, we dispatch an appropriate event. Of course we also stay aware of fringe cases (like if the user cancels the installation) and likewise dispatch appropriate events.

Hopefully that’ll give you some good direction as you start your journey pairing your AIR app with Java. Now go make something freaking fetching awesome.

Tags: , , , , , , ,


06.26.2010 / El Jeffe said:

Hey Aaron – how did you package an AIR app as “.exe”? I

06.26.2010 / Aaron Hardy said:

Hey El Jeffe,

AIR 2.0 provides a binary named adt that will package it as an exe (Windows), dmg (Mac), deb or rpm (Linux) for you. Here’s Adobe’s documentation on it:

And here’s the actual command I run on my machine that bundles the JavaUtils sample app:

Note that you have to run adt on the operating system for which you are creating the executable. So if you want an exe, you’ve got to run adt on Windows, etc.

08.05.2010 / Aaron Hardy said:

@El Jeffe, you can now create an exe by going to “Export Release Build” and choosing “Export to native installer”. Yay for no more adt command line.

09.01.2012 / CJ Moseley said:

Hmmm… Odd… Just tried the demo on a dell mini 9 (the wife’s netbook) and despite having JRE 6 and 7 installed JavaUtils denied any knowledge. Now would that be a Windows XP issue, a Java issue, and AIR issue or what… Any ideas as I’d really love to be able to build an integrated Java/Air app for talking to a MIDI drum kit/Keyboard from the netbook, but alas…

09.09.2012 / Aaron Hardy said:

CJ, what do you get as output when you run “java -version” on the command line?

09.09.2012 / CJ Moseley said:

It reports:
java version “1.7.0_07”
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)

Leave a Comment

Your email address is required but will not be published.