javascriptjavaandroidrhino

How to import java packet in javascript when using rhino-android script Engine in Android development?


I'm trying to migrate a PC java project to android. The project structure looks like:

https://github.com/speedyHKjournalist/CosmicAndroid/tree/main

Javascript files are under directory: app/src/main/assets/scripts

Java files are under directory: app/src/main/java

For scriptEngine, I'm using rhino-android in app/build.gradle. Now from java class, invoking js functions has no issue.

My question is that how to import java packet from javascript file ?

For example in Javascript:

const LifeFactory = importPackage('server.life.LifeFactory');

Error message indicates that rhino is unable to locate the java class file in android:

javax.script.ScriptException: org.mozilla.javascript.EvaluatorException: Function importPackage must be called with a package; had "server.life.LifeFactory" instead. (<Unknown source>#46) in <Unknown source> at line number 46
2023-10-24 20:11:04.063 29240-29272 System.err              com.mapleserver                      W      at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:330)
2023-10-24 20:11:04.065 29240-29272 System.err              com.mapleserver                      W      at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:288)
2023-10-24 20:11:04.065 29240-29272 System.err              com.mapleserver                      W      at scripting.SynchronizedInvocable.invokeFunction(SynchronizedInvocable.java:32)
2023-10-24 20:11:04.066 29240-29272 System.err              com.mapleserver                      W      at scripting.event.EventManager.lambda$schedule$0(EventManager.java:172)
2023-10-24 20:11:04.066 29240-29272 System.err              com.mapleserver                      W      at scripting.event.EventManager.$r8$lambda$AR51vuvnFxSsSXFZIGDAyGdIYwk(Unknown Source:0)
2023-10-24 20:11:04.066 29240-29272 System.err              com.mapleserver                      W      at scripting.event.EventManager$$ExternalSyntheticLambda0.run(Unknown Source:6)
2023-10-24 20:11:04.067 29240-29272 System.err              com.mapleserver                      W      at scripting.event.scheduler.EventScriptScheduler.runBaseSchedule(EventScriptScheduler.java:80)
2023-10-24 20:11:04.067 29240-29272 System.err              com.mapleserver                      W      at scripting.event.scheduler.EventScriptScheduler.lambda$new$0(EventScriptScheduler.java:47)
2023-10-24 20:11:04.068 29240-29272 System.err              com.mapleserver                      W      at scripting.event.scheduler.EventScriptScheduler.$r8$lambda$s8G568Z6xysfSNUGhJqxUwX71R0(Unknown Source:0)
2023-10-24 20:11:04.068 29240-29272 System.err              com.mapleserver                      W      at scripting.event.scheduler.EventScriptScheduler$$ExternalSyntheticLambda1.run(Unknown Source:2)
2023-10-24 20:11:04.068 29240-29272 System.err              com.mapleserver                      W      at server.TimerManager$LoggingSaveRunnable.run(TimerManager.java:145)
2023-10-24 20:11:04.068 29240-29272 System.err              com.mapleserver                      W      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
2023-10-24 20:11:04.069 29240-29272 System.err              com.mapleserver                      W      at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:307)
2023-10-24 20:11:04.069 29240-29272 System.err              com.mapleserver                      W      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:302)

Solution

  • This should do the trick (as per https://rhino.github.io/tutorials/scripting_java/#accessing-java-packages-and-classes), assuming your package name is indeed 'server.life.LifeFactory':

    const LifeFactory = importPackage(Packages.server.life.LifeFactory);
    

    EDITED: If 'server.life.LifeFactory' is a class, then use importClass(Packages.server.life.LifeFactory);