Googling around the web I found the Merapi AIR-Java Bridge really interesting.  I created a screen capture application using the AIR window to capture screenshots of the windows desktop through Java [http://www.youtube.com/watch?v=Gp2BzQi-gSU]. Thought of creating a simple tutorial or example of how it works.

Java

  • Create a new Java project named MerapiExample
  • Add the Merapi jar files into the library [flex-messaging-common.jar, flex-messaging-core.jar, merapi-0.0.80.jar, tools.jar].
  • Create a package inside the src folder named merapi.java

screenshot_1237182722515

Create a class named SimpleMerapi.java which implements ImessageHandler as shown below:


package merapi.java;

import merapi.Bridge;
import merapi.messages.IMessage;
import merapi.messages.IMessageHandler;
import merapi.messages.Message;

public class SimpleMerapi implements IMessageHandler {

public void handleMessage(IMessage message)
{
try
{
merapi.messages.Message msg = (merapi.messages.Message)message;
System.out.println( "Received \"" + msg.getData() + "\" from Merapi Flex" );
System.out.println( "Responding with \"Hello Merapi Flex\"\n" );

//  Instantiate a Message to respond to Merapi Flex
Message response = new Message( "simpleMerapiType", "Hello Merapi Flex" );

//  Send message to Merapi Flex
Bridge.getInstance().sendMessage( response );
}
catch(Exception exception)
{
exception.printStackTrace();
}
}

/**
* main method that instantiates the Bridge...
*/
public static void main(String[] args) {
System.out.println( "Merapi Started\n" );
//  Register MerapiHelloWorld as a listener for messages of type "helloWorldType"
Bridge.getInstance().registerMessageHandler( "simpleMerapiType", new SimpleMerapi() );
}

}

AIR

  • Create a new AIR project named MerapiAIR
  • Add the merapi-core.swc into the library

screenshot_1237184795968

  • Create a button control and a VBox containing a Label and a Text control as shown below:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:merapi="merapi.*"
layout="vertical" verticalAlign="top" horizontalAlign="center">

<merapi:BridgeInstance id="bridge" result="{responseText.text += bridge.lastMessage.data.toString()+'\n';}" />

<mx:Script>
<![CDATA[
import merapi.messages.Message;

private function onClick():void
{
bridge.sendMessage(new Message('simpleMerapiType', 'Hello from Flex!!'))
}
]]>
</mx:Script>

<mx:Button id="sayHelloButton"
label="Click to Say Hello to Merapi Java"
click="onClick()" />

<mx:VBox horizontalAlign="center">
<mx:Label text="Reponse from Merapi Java:" />
<mx:Text id="responseText" />
</mx:VBox>

</mx:WindowedApplication>
  • Now run the Java app first and then run the AIR app to the example in action.
  • It is not possible to start the Java app from AIR. So we can start the AIR app directly from Java using this Runtime.getRuntime().exec(“AIR app installtion path”); This way the AIR app will run directly from the Java app.

Note: This example was taken from the Merapi forum HelloWorld example.

Hope you enjoy it :)

Share