Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Invoking Java Process from Adobe AIR 2 Applications

0 (0 Likes / 0 Dislikes)
[ringing sound] [ADOBE DEVELOPER CONNECTION] Hello, everyone. My name is Piotr Walczyszyn. I'm a platform evangelist with Adobe. [Piotr Walczyszyn - Platform Evangelist] Today I would like to tell you about a new feature of AIR 2.0 which is called native process. [Using AIR 2 Native Process API to Communicate with Java Applications] This new features allows you to invoke native processes on user machine from AIR applications. So in order to demonstrate that, I created a simple Java project, and I will be invoking a Java process, actually. As you can see, I'm running Flash Builder with Eclipse, and it is an Eclipse plug-in. First of all, let me demonstrate for you what is here on the Java site that will listen to our calls from the AIR application. As you can see, this is very simple Java app with one static main method that will initialize a scanner. That's a Java clause that allows you to listen for some input streams. And this method of a native process on AIR site uses standard input, standard output or standard error streams in order to communicate AIR and external processes. So here I'm using a standard input to listen for the incoming requests from the AIR application. And in a while loop I'm listening either for Hello or Stop string passed from AIR app. So now let me convert that project into an AIR project. First we'll start with the Add/Change Project Type and then we'll change it to Add Flex Project Type. That's the first step. And Finish. So this will convert a Java project into a Flex project. And the next step will be to convert it into an AIR project. That's also a new feature of Flash Builder 4. So once it's done here, I do also Add/Change Project Type and Convert to Flex Desktop Project. Okay. Now in one project I have a Java code and Flex code that will run as an AIR application. First thing we have to do in order to enable native processes, we have to go into application descriptor file. Here we have to specify--I have a small cheat sheet-- we have to add element supportedProfiles. In this case, it's extended desktop. This setting allows us actually to invoke native processes, and this setting also forces us to deploy this application as a native application in a native installer. So for Windows it will be not an AIR application or AIR file as we had before, but it will be like a setup exit or on Mac it would be DMG file for that. Now if I go to my MXML code--let me switch to the source-- I want to initialize my native process when creationComplete event fires. So let me generate the handler here. Now I will define variable called process of type NativeProcess. In order to instantiate that variable, first I have to start in my creationCompleteHandler. I have to define info variable which is of type NativeProcessStartupInfo. That's a special class that holds all the necessary information about the native process that we will be running. So let me instantiate it. Okay. Now with info I set executable, so here I have to point my AIR application to the executable file on that user file system or on his drive to the file that will be executed. So user, in this case, bin and Java. That's the place where usually Java executable is on the UNIX based systems. On the Windows, you would probably go to Program, Files, Java, etc. And now another one is working directory. So that's a working directory of the process that will be running. In my case, I will use File applicationDirectory. So this is the application directory of my AIR app. Info also requires set of arguments that will be passed into my Java Virtual Machine that will be running my app. So those arguments we can pass as vector of strings. So I put Vector.<String> = new Vector.<String>(); Let's push a couple of parameters here. We can do it like that. So first parameter for Java Virtual Machine to run is a class path. So we have to define where is our Java class that will run. It will be relatively to our working directory, so in this case, application directory, one folder app, and in a bin folder. So if you look here, when I run in the debug mode, this is my application directory folder. And I go one up in bin and that's where my Java class is compiled into in this case. Next I just have to specify the name of a Java class that I will run-- net.riaspace is how I called it. And now I can assign into info.arguments parameter args. Now we are ready to actually start the process. So let's instantiate it. NativeProcess here. Let's define an event handler that event type will be ProgressEvent.STANDARD_OUTPUT_DATA, and let's call it onDataOutput. So this is the method that will be fired when something comes from the Java process or any other process, actually. And now let's start the process. As you can see, there is a Start method that takes an info parameter, which is now our NativeProcessStartupInfo. Okay. Now we define event handler, and we named it onDataOutput. And it's of type ProgressEvent. Okay. Let's read from the output stream whatever we received into a message variable that will be of type String. Okay. So from the process I can take standardOutput property, and on that standardOutput property I have readUTFBytes function that I can call, and that function takes the length in received bytes so I can read that length from the process standardOutput and bytesAvailable. Okay. Once that's done, let's call alert box. In the alert box let's pop it up with a Show function here, and let's see what we receive from Java site. The last thing, let's just get a button here. We'll put label="Call Java" and on the click event we'll just call "process." And here we'll use standardInput in order to write to the Java process. So standardInput and we will use writeUTFBytes action. And here we can pass "hello" As you remember, I showed you that in the Java code, that it will react to the Hello and return as a Hello Flex string. For this counter on the Java site, I just need to end it with the new line character. So basically, that's all. Okay. Now let's try to run it. It will run an AIR application that will invoke a Java process. Let's see. Call Java. There we go. We receive the Hello Flex string which will pop up in the alert box. So as you can see, running native processes and native applications from AIR application is fairly simple. [Adobe Developer -] But if you want to learn more, you can go to Adobe Dev Center and find the articles there but also there is an interesting post on my blog [Piotr Walczyszyn -] where I created an open source project called Flerry, and you can use it to communicate with Java processes even easier than what I showed you here. So go ahead and try it. Thank you very much. [ADOBE DEVELOPER CONNECTION]

Video Details

Duration: 10 minutes and 9 seconds
Language: English
License: All rights reserved
Genre: None
Views: 497
Posted by: adobetv on Oct 21, 2010

Piotr Walczyszyn explains the new feature in Adobe AIR 2, NativeProcess, which facilitates communication with external Java applications and your Flex projects.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.