AIR 2 Native Process with Java

Hi! Today I’m going to show you how to “mix” Java and ActionScript (I would call it JavaScri—umm no) with the help of the awesome Native Process class.
What exactly does this example app do?
It “magically” (tired of hearing that word actually) types text into a TextInput, yes types, I mean not setting the TextInput.text property, it’s done using the Java Robot class. When you click the “Hello” radio button, that word is typed, the same happens with the “Bye” radio button.

The app looks like this

The Java code is very simple, it just writes a text simulating key presses using the Robot class, if the java program receives as a parameter “Hello” it will type “Hello” in an Textfield of our application, also outoputs a string that will be read by the AIR app, making possible a two-way communnication (sorry for not having syntax highlight and formating):

public void checkForInput()

{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String text = “”;

try

{

text = in.readLine();

if (text.equals(“Hello”))

{

try

{

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_H);

robot.keyPress(KeyEvent.VK_E);

robot.keyPress(KeyEvent.VK_L);

robot.keyRelease(KeyEvent.VK_L);

robot.keyPress(KeyEvent.VK_L);

robot.keyPress(KeyEvent.VK_O);

}//robot try

catch (AWTException e)

{

e.printStackTrace();

}// robot catch

}//if = hello

if (text.equals(“Bye”))

{

try

{

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_B);

robot.keyPress(KeyEvent.VK_Y);

robot.keyPress(KeyEvent.VK_E);

}//robot try

catch (AWTException e)

{

e.printStackTrace();

}// robot catch

}

}// try in.readLine

catch(IOException e)

{

System.err.println(“Exception while reading the input. ” + e);

}// in.readLine Catch

try

{

String stringToConvert = “Lolz from Java”;

byte[] theByteArray = stringToConvert.getBytes();    

DataOutputStream dataOutputStream = new DataOutputStream(System.out);

dataOutputStream.write(theByteArray);

dataOutputStream.flush();    

}

catch(IOException e)

{     System.err.println(“Exception while reading the input. ” + e);

}

}

Now the Flexy part:

First lets set the JRE location in our machine, in this example the URL is hardcoded, and may vary depending on your OS or your JRE installation settings, you can also make the app to open a window and let you browse to the location.

(All the following is executed when the “Start Java” button is clicked)

var file:File = new File(“C:\\Program Files\\Java\\jre6”);
file = file.resolvePath(“bin/javaw.exe”);

Next we need to set te arguments in a Vector, with this, we tell JRE to execute our JAR file

var arg:Vector.<String> = new Vector.<String>;
arg.push(“-jar”);
arg.push(File.applicationDirectory.resolvePath(“MyJar.jar”).nativePath);

Now we create an instance of NativeProcessStartupinfo, this will contain our arguments vector and the location of the javaw.exe

var npInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
npInfo.executable = file;
npInfo.arguments = arg;


We have our parameters ready, now lets create a native process and start it with the npinfo that contains the necesary data to run our jar, and an event listener to receive data from Java.

nativeProcess = new NativeProcess();

nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutputData);

nativeProcess.start(npInfo);


There we go!, but don’t get excited this won’t do anything LOL so lets send some parameters:

(This code is executed when one of the radio buttons is clicked)

nativeProcess.standardInput.writeMultiByte(e.currentTarget.label+”\n”, “utf-8”);

Note: The “\n” is necessary since The in.readLine() in java considers a line when a text ends with “\n”

And w00t! That sent the parameter to Java and the text is automatically written.

Okay, but what do we do if we want to read data sent from java? easy, inside the onStandardOutputData function write:

trace(nativeProcess.standardOutput.readUTFBytes(nativeProcess.standardOutput.bytesAvailable));

And you will be able to see a message in the console everytime you click a radio button.



I’ve added the source (both FB and Java) files here

And here are some other post and articles related to Native Process:

6 thoughts on “AIR 2 Native Process with Java

  1. Not *directly*, you can only send Strings, from AIR to Java with

    nativeProcess.standardInput.writeMultiByte(“someText\n”, “utf-8″);

    then you have to manually handle that string in Java with:

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    receivedText = in.readLine();

    if(receivedText.equals( “cake” ))
    doCake();

    so depending on what’s the text received, you would add conditions in your Java program to handle it

    1. Thanks for the reply biorex21 . Actually .jar is an applet file and i have no authorization to do any change in the code of applet. i have to call the methods like start_abc(args) and stop_abc() from this applet as per my need.how can i do this. Is there any way to call these methods or m doing/trying something wrong.

      Thanks
      Adarsh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s