Flerry: Flex-Java bridge for Adobe AIR 2.0

2010 January 13
tags: , ,
by Piotr Walczyszyn

Today I published Flerry on Google Code, a Flex-Java bridge that uses NativeProcess API from Adobe AIR 2.0. Using it is very simple and similar to RemoteObject API in Flex. In fact I reused libraries from BlazeDS to do AMF de/serialization on Java side and on Flex side classes like AsyncToken, RemotingMessage, AcknowledgeMessage and ResultEvent/FaultEvent for eventing.

Here is short snippet how you can use it directly in MXML:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="300" minHeight="200" xmlns:flerry="net.riaspace.flerry.*">
    <fx:Script>
        <![CDATA[
            import flash.events.MouseEvent;
 
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
 
            protected function nativeObject_faultHandler(event:FaultEvent):void
            {
              Alert.show(event.message.toString());
            }
 
            protected function addMethod_resultHandler(event:ResultEvent):void
            {
              txtResult.text = event.result.toString();
            }
 
            protected function btnAdd_clickHandler(event:MouseEvent):void
            {
              nativeObject.add(parseInt(txtValue1.text), parseInt(txtValue2.text));
            }
            ]]>
    </fx:Script>
    <fx:Declarations>
 
    <!-- Declaring NativeObject that points to 
            net.riaspace.flerrydemo.MyJavaObject class in ./jars/flerry-demo.jar -->
    <flerry:NativeObject id="nativeObject" binPath="./jars/flerry-demo.jar" 
            source="net.riaspace.flerrydemo.MyJavaObject"
            fault="nativeObject_faultHandler(event)">
 
      <!-- Declaring NativeMethod with custom result handler, for this example 
              actually result from NativeObject could be used -->
      <flerry:NativeMethod id="addMethod" name="add" 
            result="addMethod_resultHandler(event)" />
 
    </flerry:NativeObject> 
 
  </fx:Declarations>
 
  <s:HGroup verticalAlign="middle" textAlign="center" horizontalCenter="0" verticalCenter="0">
    <s:TextInput id="txtValue1" text="2" width="30" />
    <s:Label text="+" width="30" />
    <s:TextInput id="txtValue2" text="3" width="30" />
    <s:Button id="btnAdd" label="=" width="30" click="btnAdd_clickHandler(event)" />
    <s:TextInput id="txtResult" width="30" editable="false" />
  </s:HGroup>
 
</s:WindowedApplication>

Some NativeObject properties:

  • binPath – points to jar file with your Java application
  • source – points to java class that this NativeObject maps to
  • singleton – sets source class object as singleton
  • startupInfoProvider – custom implementation of net.riaspace.flerry.IStartupInfoProvider, by default Flerry comes with JavaStartupInfoProvider that tries to detect where Java is installed on users machine, in other case Error is thrown

In following posts I will explain in details how it all works, for now you can download flerry Flash Builder project with all the source code from here.

59 Responses leave one →
  1. 2010 January 14
    Breizo permalink

    Great post thanks!
    How does this compare to Merapi?

    http://code.google.com/p/merapi/

  2. 2010 January 17
    Piotr Walczyszyn permalink

    First of all Flerry will work only with AIR 2.0 applications and it uses NativeProcess API that comes with upcoming release of AIR. NativeProcess API uses STDIN and STDOUT communication where Merapi is using local sockets communication. With Merapi main problem is that you have to start Java process first before AIR app in order to be able to communicate with it and it is not possible out of the box with AIR versions prior to 2.0.

    p.

  3. 2010 January 17

    Great post. Does it mean that from now on we’ll be able to call Java classes directly from AIR applications – I mean using only installed JRE, without the need of launching anything else?

  4. 2010 January 17
    Piotr Walczyszyn permalink

    Yes, you can call Java classes directly from AIR 2.0 apps now;) Only important thing you will need to package those apps into native installer – that is obligatory when using NativeProcess API… You can create NI directly from AIR 2.0 sdk with adt tool, EXE for windows platforms, DMG for Mac OS X, DEB or RPM for Linux distros…

  5. 2010 January 19
    Chris permalink

    Interesting stuff. But I’m still not clear on what needs to be in the JAR file… does at least one of the classes in the JAR file have to have a “main” method?

    In your example, the Java class MyJavaObject does not have a “main” method.

    With Flerry, can I call any public method of any public class in the JAR file?

    Since Flerry is using the NativeProcess API in AIR 2, Flerry must be executing something… what is that something?

    Thanks!

  6. 2010 January 20
    Zilogat0r permalink

    Bloat, bloat, bloat, corporate bloat, lame workarounds and brainless code-monkey gluing. How about Python-Lua-Java -PL/SQL-PHP-AJAX-RUPP-XML-RubyOnRAILS bridging, of course with all installers bound with it, as a nice 2GB redistributable fancy package to print “hello world”?

  7. 2010 January 20
    Piotr Walczyszyn permalink

    @Chris The jar file must contain a Java class with public methods that you can call directly from AS3. This class doesn’t require main method…

  8. 2010 January 30

    Awesome! I was playing around with jars and Native Process but was still going the Socket-Route. Thanks for sharing!

  9. 2010 February 5
    Dan permalink

    Thanks for sharing. I was wondering about how the de/serialization worked. Now I know.

  10. 2010 February 5
    Sawan Ruparel permalink

    Thanks,

    I was trying an example with a abc.jar
    Now here my class is a ParserSingleton and the method is getParsed

    so I have configured parameters as
    binpath > ./jars/abc.jar
    source > an.je.exit.ParserSingleton
    singleton > true
    id > nativeObj

    called the function by nativeObj.getParsed();

    It says class not found ? Any clue?

    Thanks,
    Sawan

  11. 2010 February 5
    Piotr Walczyszyn permalink

    Make sure your abc.jar gets copied into bin-debug/jars if you are running in debug mode (and it has an.je.exit.ParserSingleton class). What OS are you on?
    –Piotr

  12. 2010 February 5
    Sawan Ruparel permalink

    Hi,

    I could resolve the above issue, the method was public but the constructor was private. So now it seems calling the function but I am getting an error >

    RangeError: Error #2006: The supplied index is out of bounds.
    at flash.desktop::InboundPipe/readObject()

    Why would this be happening?

    Thanks,
    Sawan

  13. 2010 February 5
    Sawan Ruparel permalink

    Return type of data is some javaVO with multiple datatypes

    IN java the definition is

    public static parsedVO getParsed(filename);

    The return type is limited to string only?

    Regards,
    Sawan

  14. 2010 February 5
    Piotr Walczyszyn permalink

    Please try to change getParsed to none static method.
    Is the JavaVO marked as RemoteClass(alias=”packages.JavaVO”) on AS3 side?
    p.

  15. 2010 February 5
    Piotr Walczyszyn permalink

    … and the return type is not limited to string only.
    p.

  16. 2010 February 5
    Sawan Ruparel permalink

    Okay,

    I havent created the JavaVO on AS3

    I was expecting an object then later I will map to corresponding VO

    I am getting the RangeError on this line in the code >
    var message:AcknowledgeMessage = nativeProcess.standardOutput.readObject() as AcknowledgeMessage;

    in NativeObject.as onOutputData function

    Is AcknowledgeMessage just the trace output from java function and RemotingMessage is actual object?

    Thanks,
    Sawan

  17. 2010 February 5
    Piotr Walczyszyn permalink

    RemotingMessage is the class that is send to Java and AcknowledgeMessage is the one that is returned back.

    I just checked into SVN an example with ComplexVO that is passed to and back from Java.

    It is also available in downloads: http://flerry.googlecode.com/files/flerry-demo-1.0.1.fxp

  18. 2010 February 5
    Piotr Walczyszyn permalink

    I just tested situation with not existing VO on AS side and everything works and I receive just a dynamic Object.
    p.

  19. 2010 February 5
    Sawan Ruparel permalink

    Thanks a lot.

    I have integrated your example in my code it works :)

    But my java method doesn’t now that is definitely some problem with my Java Method. Its a complex method which parses the HTML document. Rewriting that in AS3 is not an option.

    Is there a way to debug the function called in the jar file. Some tool on desktop which can step me through the jar file while it is being executing .. or some way to route the stdout of java to as3 even if the return type is not on stdout

    Thanks,
    Sawan

  20. 2010 February 5
    Piotr Walczyszyn permalink

    There is no easy way, you can just built your own Java class with main method and invoke it as standard Java class and also use the Amf3Input and Amf3Output to do AMF de/serialization…

    How complex/big is your returning JavaVO object?
    p.

  21. 2010 February 8
    Sudarshan Kumar G permalink

    Hi Walczyszyn ,
    Thanks for the post. I am really happy to see the solution for my problem. Actually I have a requirement of an AIR Client which will connect to blazeds server and I need to call some java methods at client side. By seeing this post, I am eagerly waiting to integrate flerry into my project and see. But I didn’t find any documentation of it. I am working on Flex Builder. Can I import this project into Flex Builder. Can you please guide me to do the same.

    Thanks & Regards,
    Sudarshan Kumar G

  22. 2010 February 10
    Sawan Ruparel permalink

    Sorry, was caught up in the bugs :)

    Its a huge function

    Thanks for all the help, let me know if you are looking to develop some enhancement to this framework. I would like to contribute to it.

    ~ Sawan

  23. 2010 February 12
    Piotr Walczyszyn permalink

    @Sudarshan Kumar G to communicate AIR app with Java/BlazeDS backend you don’t need Flerry. You can do it out of the box with Flex using RemoteObject class.

    Here is the documentation for that: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_4.html

    and ASDoc here: http://livedocs.adobe.com/flex/3/langref/mx/rpc/remoting/mxml/RemoteObject.html

  24. 2010 February 13
    Martin permalink

    Thanks . this was exactly that i was searching for. Is there any benchmark available between the performance of AS3 and Java code performance and memory foot prints ?

    I need to develop a Rich client server application as its very processor oriented bussiness computation,so cannot find any nice wat to have a middle tier until there is a lot of investment on hardware. So thought of flex and java bussiness boths at client side; was searching something to talk between java and AIR smoothly .. thanks .. will sure try this today

    Any siggestions and comments are welcome for my scenario

  25. 2010 February 13
    Pawan permalink

    Hi Piotr,

    I am basically from Java background and a beginner to flex. I thing your solution will help me a lot. But I am not able to run the project. (Details of error message: http://picasaweb.google.co.in/D.pawan05/Other#5437637183425455874)
    Does it need some special configuration? I am using:
    - Flex Builder 3.
    - JRE and JDK Version 1.6.
    - Operating System: Windows XP SP 2
    Please provide more information for necessary support/compatibility and configurations .

    Waiting for reply.

  26. 2010 February 13
    Pawan permalink

    tip: The link of the error message:
    http://picasaweb.google.co.in/D.pawan05/Other#5437637183425455874

    Thank you.

  27. 2010 February 13
    Piotr Walczyszyn permalink

    In order to run it you will need Flash Builder 4 with Flex 4 and AIR 2.0 runtimes. Flerry is using new API’s that are coming with AIR 2.0.

  28. 2010 February 15
    Sangita permalink

    Hi Piotr,
    I am a beginer in Flex.Hope ur code will help me in proceeding with my work

    Thank you.

  29. 2010 February 15
    Sangita permalink

    Hi Piotr,

    Thanks for sharing.Just wanted to know how Flerry differ from Merapi and what are its advantages.

    Waiting for ur Reply

    Thanks

  30. 2010 February 15
    Pawan permalink

    Hi Piotr,

    I have installed Flash Builder 4. (Since Flerry works with Flex 4)
    Then I have imported Flerry Demo (flerry-demo-1.0.1.fxp) in Flash Builder.
    While I tried to build the project, It is giving an error saying:
    ‘Type was not found or was not a compile-time constant: NativeProcessStartupInfo’
    When I tried typing – import flash.desktop. – the NativeProcessStartupInfo wasn’t there on pop-up list.
    Another thing is it says ‘Unable to open c:\Documents and Settings\UserName\Adobe Flash Builder Beta 2\Flerry\bin-as3\Flerry.swc’
    Details of error message: http://picasaweb.google.co.in/D.pawan05/Other#5438539678913349794
    Does it need to set up libraries or need to make some configuration?

    Thanks. Waiting for your reply.

  31. 2010 February 16
    Pawan permalink

    Hi Piotr,

    I got the solution. The thing was I had not merged the AIR 2 sdk with flex 4 sdk. That’s why I was getting the error message.

    Those who are facing the same problem please visit this thread on Adobe forum:
    http://forums.adobe.com/thread/574872?tstart=0

    Thanks.

  32. 2010 February 16
    Pawan permalink

    Hi Piotr,

    There is new problem.

    Problem: Flash Builder 4 (beta) crashing unexpectedly.
    Event: Opening the Design view.
    Preconditions: Added library project ‘Flerry’.
    Description: When I have tried to open the Design view to add the controls the Flash Builder is terminating without any prompt. And special thing is this is happening only when I add the ‘Flerry’ as a library project.

    Did you encountered this problem before?
    Is this a bug in Flash Builder?

    Reply.

  33. 2010 February 22
    Sudarshan Kumar G permalink

    Hi Walczyszyn,
    Thanks for your valuable reply. Actually, I need to call a java print method, which resides at client side but not at server side. In this case, I need flerry bridge to call such methods. Anyway, I solved the issue by reading this post once again. Thanks for the post. It helped me a lot.

    Thanks & Regards,
    Sudarshan Kumar G

  34. 2010 February 22
    Sudarshan Kumar G permalink

    Hi Walczyszyn,
    I am encountered with some problem. I am working on Windows XP Operating system. When I saw your source code, you are finding java in C:/Program Files/java, if the OS is windows. But what happens when the java installation is in another drive or another path?. My java installation is in another drive. Now I am not able to call java methods with flerry bridge. Is there a way to find java automatically without hardcoding the path, just like by searching java in registry or something? Please correct me If I am wrong and help me to do the same.

    Thanks & Regards,
    Sudarshan Kumar G

  35. 2010 February 25
    Victor permalink

    Hi Piotr
    Thanks a lot. I just made it work with Flex Builder 3 and Air 2.0 beta 2 sdk.
    Amazing Work.

    Greetings from Peru

    - Victor Muguerza Capristan

  36. 2010 February 27
    Victor permalink

    Hi Piotr:

    I have a question, if i have 2 clases let say one person and other personController in my Java side, i have to declare two different native object for each one in the Air side ?

    Thanks for your response

    - Victor Muguerza Capristan

  37. 2010 February 27
    Piotr Walczyszyn permalink

    If Person class is only Value Object you don’t need to declare NativeObject for it, only for personController that has some logic that you want to invoke. For Person you most probably would declare AS3 equivalent annotated with [RemoteObject] metadata.

  38. 2010 February 27
    Victor permalink

    Hi Piotr:
    Tranks for your valuable reply. I actually made a lots of test with flerry but i have a problem trying to get data from a database (using mysql actually).
    I get the following error:

    RangeError: Error #2006: The supplied index is out of bounds.
    at flash.desktop::InboundPipe/readObject()
    at net.riaspace.flerry::NativeObject/onErrorData()[/Users/pwalczys/Workspaces/Blog/flerry/src-as3/net/riaspace/flerry/NativeObject.as:99]

    Can you provide a simple example of flerry with db or tell me what steps should i follow.
    Tranks for your response:

    -Victor Muguerza Capristan

  39. 2010 March 2
    Prashant permalink

    Hi Piotr:
    I have successfully done with calling my own java api using flerry just like the ‘add’ method that you have implemented.

    But now I want to create an object of a class. The constructor of the class is having some parameters to initialize the object. “How can i specify the parameters to the constructor?”.

    can you provide me with any documentation about the working of flerry?
    waiting for your reply…

    Thanks and regards,
    Prashant

  40. 2010 March 2
    Piotr Walczyszyn permalink

    With current version of Flerry java class that is mapped to NativeObject may not have any constructor parameters.

  41. 2010 March 2
    Prashant permalink

    “With current version of Flerry java class that is mapped to NativeObject may not have any constructor parameters.”

    But why is it so?
    would it be possible in further version of flerry?
    or can we make it possible using the concepts used in flerry and some other similar things?

  42. 2010 March 2
    Sangita Patel permalink

    I have imported Flerry Demo (flerry-demo-1.0.1.fxp) in Flash Builder beta 2.

    I have also merged AIR 2 with it.

    I am not getting any compile error,but rather I am getting a run time error while running the Flex Demo in my laptop.

    Details of error: RangeError:Error#2006:The Supplied index is out of bound and Could not find the main class.

  43. 2010 March 2
    Sangita Patel permalink

    I have reinstalled Flex builder 4 and performed the same task again but I am getting the same error.

    I have also tried to install it in another Drive and change the workspace then also the problem is not solved

    Waiting for ur Reply

    Thanks and Regards
    Sangita

  44. 2010 March 2
    Piotr Walczyszyn permalink

    @Sangita: Do you run Flash Builder as Eclipse plug-in? The example requires it to compile Java source.

    What operating system you are on?

  45. 2010 March 2
    Piotr Walczyszyn permalink

    @Prashant: Technically it is not a problem to implement constructor parameters support. It is just matter of having a time to do that ;) If you want to help me with that I can give you check-in rights to SVN on Google Code.

  46. 2010 March 2
    Sangita Patel permalink

    hi

    Piotr

    Right now I am on Windows XP

    How to get rid of the problem

    Waiting for ur reply

    Thanks and Regards
    Sangita

  47. 2010 March 2
    Piotr Walczyszyn permalink

    Where do you have Java installed on your WinXP box?

  48. 2010 March 2
    Sangita Patel permalink

    Hi
    Piotr

    I have installed Java on C:\Pogram Files\Java\jre1.6.0_01

  49. 2010 March 2
    Sangita Patel permalink

    Hi Piotr

    The same Flerry Demo(flerry-demo-1.0.1.fxp)is executing in my office PC.
    Is there any problem related to configuration or is related to memory issue.
    I am getting following error

    RangeError: Error #2006: The supplied index is out of bounds.
    at flash.desktop::InboundPipe/readObject()
    at net.riaspace.flerry::NativeObject/onErrorData()[C:\Documents and Settings\Sangita\Adobe Flash Builder Beta 2\flerry\src-as3\net\riaspace\flerry\NativeObject.as:99]

    Waiting for ur Reply

    Thanks and Regards
    Sangita

  50. 2010 March 2
    Piotr Walczyszyn permalink

    Probably is a problem of location where you have Java installed on home PC.
    Please check and let me know where you have it installed.

Trackbacks & Pingbacks

  1. Java-AS3 serialization with AMF | Space of Flex/AIR technologies
  2. 使用Merapi来在AIR和Java中通信 | Color Hook
  3. MikeCann.co.uk » Blog Archive » On the Bleeding Edge

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS