Flerry: Flex-Java bridge for Adobe AIR 2.0
2010 January 13
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.





Great post thanks!
How does this compare to Merapi?
http://code.google.com/p/merapi/
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.
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?
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…
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!
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”?
@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…
Awesome! I was playing around with jars and Native Process but was still going the Socket-Route. Thanks for sharing!
Thanks for sharing. I was wondering about how the de/serialization worked. Now I know.
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
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
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
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
Please try to change getParsed to none static method.
Is the JavaVO marked as RemoteClass(alias=”packages.JavaVO”) on AS3 side?
p.
… and the return type is not limited to string only.
p.
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
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
I just tested situation with not existing VO on AS side and everything works and I receive just a dynamic Object.
p.
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
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.
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
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
@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
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
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.
tip: The link of the error message:
http://picasaweb.google.co.in/D.pawan05/Other#5437637183425455874
Thank you.
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.
Hi Piotr,
I am a beginer in Flex.Hope ur code will help me in proceeding with my work
Thank you.
Hi Piotr,
Thanks for sharing.Just wanted to know how Flerry differ from Merapi and what are its advantages.
Waiting for ur Reply
Thanks
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.
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.
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.
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
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
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
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
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.
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
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
With current version of Flerry java class that is mapped to NativeObject may not have any constructor parameters.
“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?
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.
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
@Sangita: Do you run Flash Builder as Eclipse plug-in? The example requires it to compile Java source.
What operating system you are on?
@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.
hi
Piotr
Right now I am on Windows XP
How to get rid of the problem
Waiting for ur reply
Thanks and Regards
Sangita
Where do you have Java installed on your WinXP box?
Hi
Piotr
I have installed Java on C:\Pogram Files\Java\jre1.6.0_01
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
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.