Myna Embdedding

Skip to first unread message

Eric Charles

May 13, 2011, 11:36:14 PM5/13/11
to MynaJS-General

I had [1] and [2] from a conversation with Tony Zakula on the way Myna
could be embedded in a JAVA application to run Javascript.

I see in JsCmd that a Myna Thread can be called and the Myna root file
must be provided.

- Is there a way to load the js files via classpath (the js files
being a jar artifact) ?
- Is there a way to instanciate Myna as plain object without going via
the MynaThread?
- I understand Myna uses Rhino. If I "simply" need to run JavaScript
from JAVA, what would be the Myna added value?

- Eric


Mark Porter

May 14, 2011, 11:23:07 AM5/14/11
to MynaJS-General
Myna is designed to dynamically compile jsvascript files from the
filesystem in a web environment. That said, it can actually compile
and run raw javascript text, or theoretically use resource URL's since
all file handling id via URL/URI.

MynaThread is as "plain" as it gets. It is the core runtime class that
does all the Rhino interaction. All of the application server related
code is actually in JS files, so that can be heavily modified without
editing MynaThread. For example, all the user management and
permissions can be removed.

What Myna adds over plain Rhino, is it's standard library. Here is a
quick example of a plain Rhino task made easier in Myna:

Converting a Java bean to a JSON object
---- Plain Rhino----
var bean = new org.custom.SomeClass();
var result={}
for (x in bean){
if (/^get./.test(x)){
property=property.substr(0,1).toLowerCase() + property.substr(1);
result[property] = bean[x]();
if (result[property] instanceof java.lang.String){
result[property] = result[property];

if (/^is./.test(x)){
property=property.substr(0,1).toLowerCase() + property.substr(1);
result[property] = bean[x]();
if (result[property] instanceof java.lang.String){
result[property] = Boolean(result[property]);


//make sure to reverse the slashes for Windows, or make a URI first
var fstream = new"/path/to/app/dir/jsonfiles/
var out = new;
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
} finally{

---- Myna ----
var bean = new org.custom.SomeClass();
var result = JavaUtils.beanToObject(bean).toJson();
// here, "/" is the rootDir for Myna, and will work in UNIX and
new File("/jsonfiles/filename.json").writeString(result);

Myna also manages things like caching compiled JS code, and re-using
scopes to speed up repeated tasks

If you have no interest general purpose scripting, you can simplify
things by making your own Rhino runtime, or modifying Myna's JS
runtime. This will require either modifying Tony's code to not use
Myna's libraries, or load just enough of the Myna libs to get the code
to work. If I could see Tony's code I could probably give some
pointers on how big a task it would be convert.
> [2]

Tony Zakula

May 14, 2011, 11:49:13 AM5/14/11
Yes, I will try to clean up some extraneous details in it and post it
somewhere this week.

Tony Z

> --
> You received this message because you are subscribed to the Google Groups "MynaJS-General" group.
> To post to this group, send email to
> To unsubscribe from this group, send email to
> For more options, visit this group at

Tony Zakula

May 15, 2011, 10:46:05 PM5/15/11
Hi Eric and all,

I have posted some basic code for parsing emails using Mime4J and JavaScript at


Tony Z

Reply all
Reply to author
0 new messages