Log
View Options
AS3 to Clipboard to JAVA Communication
2/26/10

After having trouble finding a good example of how to pass information between an ActionScript module and JAVA module I came up with the fallowing solution; Instead of using direct communication (which I was having all kinds of problems with) I simply used the clipboard as a median between the two modules.
-
You will need the fallowing:
- Something that can compile ActionScript as .exe applications. I'll be using Adobe Flash CS4.
- Something that can compile .jar applications. I'll be using NetBeans.
- YOUR BRAIN! I had to use mine :)
What we will do:
We are going to build a simple AS3 application that sends data to the clipboard and then runs a .bat file. This .bat file executes a .jar file that grabs clipboard data and tries to make a file out of it.
Ok, let's get started. First thing, lets build our flash interface. I'll be naming the flash module AStoBatch. Make two input text boxes, call one _file and the other _content. Make sure _file is set for single line only, otherwise it sends some funk to the clipboard that will confuse our JAVA app later. Make a MovieClip (this will be our button to send the data) and name it _build. Ok, now lets add our functionality, put this in you main class package:
Snipt.net is dead. Replace link to "http://snipt.net/embed/8083620d18ef18c98238730d6fc0bf40"
Now we need to export our flash module to an .exe, using Flash CS4 all you need to do is check the box in your Publish Settings and then Publish the project. Why do we have to make it an .exe? Well the AVM will not let .swf files execute/run our important fscommand. This can only be done from .exe for security reasons. Sweet! Let us move on to building our simple .bat file. Now the important thing to know here is, any call from an AS3 module to fscommand must reffer to a relative folder called fscommand. So in the same folder you just published your AS3 app to, make a folder called fscommand. Ok, now make a new text file in our fscommand folder and rename the file to RunJar.bat. For those of you unfamiliar with .bat(batch) files, they are terminal files (command prompt) that execute a series of commands. In our RunJar.bat file drop this line.
java -jar "../dist/BuildFromCBD.jar"
Ok, so all we are saying there is: go back to the root folder, go to the dist folder (will be made later) and execute the BuildFromCBD.jar (will be made later). Now is that time to make our .jar file. Open NetBeans (freeware) or what ever your pleasure for JAVA scripting is. Make a new project called BuildFromCBD. And drop this code in your main project file:
Snipt.net is dead. Replace link to "http://snipt.net/embed/69fe6937b5d950359a47f8685043f0ff"
Now compile the .jar and you will have (if using NetBeans) a folder called dist (the one our .bat file points to) and in it will be the BuildFromCBD.jar file. Super sweet! Now I'm not going to explain every piece of code for you, but If you would like, DOWNLOAD THE EXAMPLE PACKAGE and there you will find the proper files all commented for your understanding.