Scripting(JS) in Java 6

JavaScripting.java

package taher.javahunter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 *
 * @author taher
 */
public class JavaScripting {

    public static void main(String[] args) {
        JavaScripting javaScripting = new JavaScripting();
        try {
            javaScripting.javaHunter_JAVASCRIPTING();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public void javaHunter_JAVASCRIPTING() throws ScriptException, NoSuchMethodException, FileNotFoundException {
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine engine = m.getEngineByName("javascript");
        if (engine != null) {
            InputStream is = new FileInputStream("C:/browser.js");
                    // this.getClass().getResourceAsStream("browse.js");
            Reader reader = new InputStreamReader(is);
            engine.eval(reader);
            Invocable invocableEngine = (Invocable) engine;
            System.out.println("Return : "+invocableEngine.invokeFunction("taher"));
            invocableEngine.invokeFunction("sum",1,2,3);
            invocableEngine.invokeFunction("loop",3);
            invocableEngine.invokeFunction("array");
            //engine.eval("println('Hello')");

            /*
             * Miscellaneous Codes
             */
            //1.
            Object obj = engine.get("obj");
            invocableEngine.invokeMethod(obj, "hello", "Script Method !!" );

            //2.
            File f = new File("C:/JavaHunter.taher");
            // expose File object as variable to script
            engine.put("file", f);
            // evaluate a script string. The script accesses "file"
            // variable and calls method on it
            engine.eval("println('File size in KB '+(file.length()/1024))");

            //3.
            Runnable r = invocableEngine.getInterface(Runnable.class);
            // start a new thread that runs the script implemented
            // runnable interface
            Thread th = new Thread(r);
            th.start();

        }
    }
}

browser.js

function taher(){       
    println('JavaHunter');
    return true;
}

function sum(temp1,temp2,temp3){
    println('temp1 : temp2 : temp3 : '+temp1+' : '+temp2+' : '+temp3);
    println('Sum : '+eval(temp1+temp2+temp3));
}

function loop(cnt){
    for(var i=0;i<cnt;i++){
        println('Loop : '+i);
    }
}

function array(){
    var a = java.lang.reflect.Array.newInstance(java.lang.String, 2);
    a[0] = "Taher";
    a[1] = "JavaHunter";
    println('Array Length : '+a.length);
    for(var i=0;i<a.length;i++){
        println('a['+i+'] : '+a[i]);
    }
}

var obj = new Object();
obj.hello = function(name) {
    println('Hello, ' + name);
}

function run() {
    println('Run called');
}

OutPut

JavaHunter
Return : true
temp1 : temp2 : temp3 : 1 : 2 : 3
Sum : 6
Loop : 0
Loop : 1
Loop : 2
Array Length : 2
a[0] : Taher
a[1] : JavaHunter
Hello, Script Method !!
File size in KB 3.3984375
Run called
This entry was posted in Scripting in JAVA. Bookmark the permalink.

4 Responses to Scripting(JS) in Java 6

  1. Sanjay Prajapati says:

    Good one buddy…

  2. kapil says:

    on first glance looks quiet attractive! will the processes guaranteed to be closed or would they remain running? are there any bottleneck on this?
    any surprises ? would be nice to know if this has been tested with many users concurrently running separate http server sessions etc, does it slow on any number of users?
    http://stackoverflow.com/questions/5688585/how-to-use-wkhtmltopdf-in-java-web-application/6085001#6085001

  3. kapil says:

    Pl help, I am not able to get the exe file running.
    I need this to work urgently
    06:55:51,206 INFO [STDOUT] Exception::java.io.IOException: Cannot run program “wkhtmltopdf.exe”: CreateProcess error=2, The system cannot find the file specified

    Do I need to put exe file in the same folder as the GeneratePDF file or is it placed elsewhere? How do I read the location of exe file in java code, the runt

Leave a reply to tahertinwala Cancel reply