Using Rhino instead of ScriptEngine to run Javascript code in java

228 views
Skip to first unread message

learner via StackOverflow

unread,
Oct 17, 2015, 8:40:07 PM10/17/15
to google-appengin...@googlegroups.com

Based on the discussion converting string representation of unknown date-format to Date in java, I want to use the JavaScript Date function in my App-Engine project. However, ScriptEngine does not work on App-Engine. So I need a little help converting to Rhino. so here is the ScriptEngine code I need to convert:

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

ScriptEngine engine = scriptEngineManager.getEngineByName("JavaScript");
String script = "var date = new Date('" + dateInUnknownFormat + "');
var timestamp = date.getTime();";


 engine.eval(script); 
 long timestamp = ((Double) engine.get("timestamp")).longValue();

The following has not worked

private static long parseDateUsingRhino(String dateInUnknownFormat){
        Context mozillaJsContext = Context.enter();
        Scriptable scope = mozillaJsContext.initStandardObjects();
        String script = "var date = new Date('" + dateInUnknownFormat + "'); var timestamp = date.getTime();";
        Object obj = mozillaJsContext.evaluateString( scope, script, "TestScript", 1, null );
        Double timeDouble = Double.parseDouble((String) obj);
        long timestamp = timeDouble.longValue();
        return  timestamp;
    }

and I have already replaced "TestScript" with null and "".

Here is the error trace:

Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
     at java.lang.ClassLoader.defineClass(ClassLoader.java:300)
     at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:27)
     at org.mozilla.javascript.optimizer.Codegen.defineClass(Codegen.java:130)
     at org.mozilla.javascript.optimizer.Codegen.createScriptObject(Codegen.java:85)
     at org.mozilla.javascript.Context.compileImpl(Context.java:2394)
     at org.mozilla.javascript.Context.compileString(Context.java:1335)
     at org.mozilla.javascript.Context.compileString(Context.java:1324)
     at org.mozilla.javascript.Context.evaluateString(Context.java:1076)
     at com.mycompany.android.MainActivity.parseDateUsingRhino(MainActivity.java:52)
     at com.mycompany.android.MainActivity.onCreate(MainActivity.java:45)
     at android.app.Activity.performCreate(Activity.java:6221)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728) 
     at android.app.ActivityThread.access$900(ActivityThread.java:172) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:145) 
     at android.app.ActivityThread.main(ActivityThread.java:5837) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372)


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/33192485/using-rhino-instead-of-scriptengine-to-run-javascript-code-in-java

Filip via StackOverflow

unread,
Oct 22, 2015, 6:31:09 PM10/22/15
to google-appengin...@googlegroups.com

You should try it first like this and see if it works:

private static long parseDateUsingRhino(String dateInUnknownFormat){
        Context mozillaJsContext = Context.enter();
        Scriptable scope = mozillaJsContext.initStandardObjects();
        String script = "var date = new Date().getTime();";
        Object result = mozillaJsContext.evaluateString( scope, script, "", 1, null );
        return Long.valueOf(Context.toString(result));
    }


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/33192485/using-rhino-instead-of-scriptengine-to-run-javascript-code-in-java/33291839#33291839

Filip via StackOverflow

unread,
Oct 22, 2015, 6:41:11 PM10/22/15
to google-appengin...@googlegroups.com

You should try it first like this and see if it works:

private static long parseDateUsingRhino(String dateInUnknownFormat){
        Context mozillaJsContext = Context.enter();
        Scriptable scope = mozillaJsContext.initStandardObjects();
        String script = "var date = new Date().getTime();";
        Object result = mozillaJsContext.evaluateString( scope, script, "", 1, null );
        return Long.valueOf(Context.toString(result));
    }

And according to this all you need to do next is pass the dateInUnknownFormat variable like so, no java string concatenation required (the context is smart enough to probably consider the method accessible variables as global variables in the javascript scope):

String script = "var date = new Date(dateInUnknownFormat).getTime();";

Filip via StackOverflow

unread,
Oct 23, 2015, 3:16:08 AM10/23/15
to google-appengin...@googlegroups.com

You should try it first like this and see if it works:

private static long parseDateUsingRhino(String dateInUnknownFormat){
        Context mozillaJsContext = Context.enter();
        Scriptable scope = mozillaJsContext.initStandardObjects();
        String script = "var date = new Date().getTime();";
        Object result = mozillaJsContext.evaluateString( scope, script, "", 1, null );
        return Long.valueOf(Context.toString(result));
    }

And according to this all you need to do next is pass the dateInUnknownFormat variable like so, no java string concatenation required (the context is smart enough to probably consider the java method's accessible variables as global variables in the javascript scope):

Reply all
Reply to author
Forward
0 new messages