Simon is an Adobe Community Expert, an Adobe Certified Instructor, and a Senior Web Developer for Working on several high profile projects ranging from a start-up athlete skin care shopping cart application to a full blown CMS requiring multiple workflows for publishing and editing several types of content, Simon has used many different technologies including ColdFusion, Flex, InDesign, AIR, and Blaze DS. Constantly learning, Simon is always looking for new challenges and reports his findings to user groups and on his website. Simon has posted 3 posts at DZone. View Full User Profile

Merapi : A Bridge Between AIR and Java

  • submit to reddit

The Flex Stuff

Now that the Java is in place, let's move onto building our AIR application.

The files for the AIR application are similar to the Java ones in structure.  There is a main application file, and there is a message class, but instead of having a handler class, that functionality is often just placed wherever the Merapi functionality is located. 

First thing you need to do to build the AIR application is to create a new project in Flex Builder.  When creating the project, remember to select that it is a Desktop Application.  When you get to the library section, add the SWC files you checked out from the Merapi Repo.

Once you have created your project, open up your main MXML file and place the following code in the file.

<?xml version="1.0" encoding="utf-8"?>
type="{ questionMessage.ASK_IT }"

import merapi.messages.IMessage;
import merapi.messages.Message;
import com.magiceightball.messages.*;

private function askIt():void{
var q:questionMessage = new questionMessage();
q.question = question.text;

private function handleResult(e:ResultEvent):void{
var message:questionMessage = e.result as questionMessage;
answerReceived.text = message.answer;
questionAsked.text = message.question;

<mx:Label x="40" y="42" text="What is your question?"/>
<mx:TextInput x="181" y="40" id="question"/>
<mx:Button x="108" y="94" label="Ask" click="askIt()"/>
<mx:Label x="150" y="168" id="answerReceived"/>
<mx:Label x="40" y="142" text="Question Asked:"/>
<mx:Label x="40" y="168" text="Answer Received:"/>
<mx:Label x="150" y="142" id="questionAsked"/>


Upon reviewing this code, you will see a lot of similarities between this code and the Java code.  Part of that is due to the similarities between ActionScript and Java, and part of it is due to Merapi making use of objects which in general are very similar from language to language.  The biggest difference you will see in the Flex code is that instead of having a separate handler class, this code specifies a message handler through the use of the MessageHandler tag.  In this tag we specify the type of message it is handling and what function to call when a message is received.  If you are building an application that will use different types of messages, you will need to use multiple MessageHandler tags.  In this example just like in the Java example, the message type is stored in the message class as a static variable.  In this case the message class is called questionMessage.  The value of this variable, just like in the Java example is magicQuestion.

The askIt() function is the function used to build and send a message to Java.  This function creates an instance of questionMessage and populates it with the user-supplied data.  It then calls the send function, which sends the message to the bridge.  Once in the bridge, the message can then be picked up by any other applications connected to the bridge.

The handleResult() function is called whenever a new message is received from Java.  The message is passed to the function through the resultEvent and is then cast as a QuestionMessage.  Once it is in the correct format, the answer which was generated by Java is extracted and displayed to the user.

Let's take a look at the final file we will need to create for this example, the questionMessage class.  Create a new ActionScript class called questionMessage in the path com/magiceightball/messages.  It is always the best practice to keep all message classes in a messages folder, just like you do in Java.  In the class add the following code:

package com.magiceightball.messages
import merapi.messages.Message;
[RemoteClass( alias="messages.questionMessage" )]

public class questionMessage extends Message
public static const ASK_IT : String = "magicQuestion";
public var question : String = null;
public var answer : String = null;

public function questionMessage()
super( ASK_IT );


As you can see, there is not much code required to create the message class, but the code that is in this class is very specific and must match up with the Java equivalent of this class exactly.  In this class you will notice a remoteClass function.  In it we set an alias value.  The alias value is the exact path that the questionMessage class in the Java application has.  Whenever creating a new message type, you must set an alias that maps it to where its Java equivalent is located.  In the Java code, we run a comparison to make sure that we are only dealing with objects of the same type.  Using the alias makes sure that Java will see this object as the same.  While you are developing your application, if you ever have an instance where you are firing off messages from your AIR application and Java is not receiving them, check the alias, since this will most likely be the root of the problem.  Many an hour has been lost to this single line of code.

This class must match also its Java counterpart's variables.  You will notice that this class has question and answer variables and that the Java class has get/set question and get/set answer methods.  It is important that both objects have the same methods, but since ActionScript automatically generates getters and setters for its public variables, you only need to create the variables in the ActionScript class.

Now that we have all the code created for this application, let's give it a whirl. The first thing you will want to do is to start up the Java application.  Go to the magicEightballRunnner file, and run it.  Once that is running error-free, run the AIR application.  You will see a text area and a button.  Type in a question, and hit the button.  If all goes well, below the button you should see the question that you asked and the response from Java.  Everything that is displayed below the button – both the question and answer – is data from the Java application.  You now have your first fully functional Merapi application!  Take some time to look around, and play with some of the methods until your are comfortable enough to make your own application.

As you can see from this example, it is very easy to create applications that communicate via the Merapi bridge.  This example alone was created in less than 45 minutes.  Opening up Java to AIR applications allows for a whole new area of application development.  Not only can the applications tie into other technologies that previously could only be accessed through web servers, but also the applications can now interact with and control devices that are accessible through serial ports and USB drives.  If you take a look at the Merapi web site, you will see that there are already a number of videos demonstrating AIR applications interacting with RFID scanners, GPS units, and even Lego Mindstorm robots.  These are but a few of the endless possibilities you have when connecting AIR applications with Java.  My question to you is: what are you going to build?

Published at DZone with permission of its author, Simon Free.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Andrew Sweeney replied on Sat, 2010/01/02 - 3:47pm

I am very new to Java programming, SVN and eclipse. I have done very little svn downloading. In the first page you say to download the code but it just takes me to a site with the code list. How do I go about actually downloading the links listed. Thanks Andy

J S replied on Sun, 2011/06/05 - 11:57am

Didn't your momma teach you to capitalize your Java/AS class names?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.