Java and JavaScript, Living Together! - Capture Club
kmcowan 16 April, 2018 0

Java and JavaScript, Living Together!

“Dogs and Cats, living together… mass hysteria!”

One of the very interesting features of a JavaScript stage in Fusion (and of Java 8/Nashorn, in general) is the ability to intermingle the two languages. By way of example, I offer the following JavaScript stage. In this case, we’re pulling from an geolocation process, and creating a query from the resulting attributes. You’ll notice at the top that we declare several Java classes, and then when we go to JSON, presto!, we switch over to JavaScript and run through our list, feeding the values into a java.util.ArrayList, and then we switch back to Java and iterate over the list and build our query. We could have used a regular old JavaScript Array, but in this case it makes a textbook illustration how the two languages can interact as one in the context of the Java ScriptEngine.

      function (request, response) {
    // java imports
    var e = java.lang.Exception;
    var URL =;
    var BufferedReader =;
    var InputStreamReader =;
    var String = java.lang.String;
   // local declarations
    var result = new String("");
    var ArrayList = java.util.ArrayList;
    try {
        var url = "*&outSR=4326&f=pjson&Single+Line+Input=799+G+St";
        var targetUrl = new URL(url);
        var ins = new BufferedReader(new InputStreamReader(targetUrl.openStream()));
        var inputLine = "";
        var list = new ArrayList();
        while ((inputLine = ins.readLine()) !== null) {
            //; // if you want to see what is on each line
            result += inputLine;
        ins.close();"Return RESULT: " + result); // full result
        // convert to JSON using JavaScript
        var json = JSON.parse(result);" ** JSON CREATED OKAY ** " + JSON.stringify(json));
        // get candidates
        var candidates = json.candidates;"Candidates: " + candidates.length);
        for (var i = 0; i < candidates.length; i++) { var candidate = candidates[i]; var refId = candidate.attributes.Ref_ID; list.add(refId);"ADD REF: " + refId); } var query = "";"*** BUILD QUERY ** "); // run through the list and build your query. if (list.size() > 0) {
            var count = 0;
            for each (var ref in list) {
                if (count > 0) {
                    query += " OR ";
                query += "FIELD_TO_QUERY:" + ref;
           // print out the query in the logs
  "**** QUERY: *** " + query);
            // add to response
            response.initialEntity.appendStringList("solr_query",, Java.type('java.util.List')));
    } catch (e) {

Why is this so odd? Well, here’s the thing: Java and JavaScript, despite sharing the similarity of the word “java” are really two entirely different beasts.
Java is a strictly-typed, procedural language (albeit with a few functional traits). JavaScript, on the other hand, is a loosely-typed functional language, with more in common with languages like LISP and Scala. So to see these two playing nice in the same sandbox is certainly a treat, and a powerful tool, indeed.

Leave a Reply