Search code examples
javaosgikarafscriptenginerenjin

Need help loading Script Engine into Karaf


Has anyone had any luck loading script engines into Karaf. I've seen some old links regarding loading script engines into OSGi containers: - https://devnotesblog.wordpress.com/2011/09/07/scripting-using-jsr-223-in-an-osgi-environment/ - Is OSGi fundamentally incompatible with JSR-223 Scripting Language Discovery?

But have had no luck thus far loading into karaf. I have simple example project of what I'm trying to do here:

https://gitlab.com/mkwyche/helpful-hints/tree/master/renjin-karaf

Each time I try to load the script. Using the following line:

        ScriptEngineManager manager = new ScriptEngineManager();

    // create a Renjin engine:
    engine = manager.getEngineByName("Renjin");
    // check if the engine has loaded correctly:
    if(engine == null) {
        throw new RuntimeException("Renjin Script Engine not found on the classpath.");
    }

I get a class not found exception:

    at java.lang.Thread.run(Thread.java:745)[:1.8.0_60]

Caused by: java.lang.RuntimeException: Renjin Script Engine not found on the classpath. at datadidit.helpful.hints.renjin.karaf.RenjinKarafTest.testRuntime(RenjinKarafTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_60] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_60] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_60] at java.lang.reflect.Method.invoke(Method.java:497)[:1.8.0_60] at org.apache.aries.blueprint.utils.ReflectionUtils.invoke(ReflectionUtils.java:299)[12:org.apache.aries.blueprint.core:1.6.2] at org.apache.aries.blueprint.container.BeanRecipe.invoke(BeanRecipe.java:980)[12:org.apache.aries.blueprint.core:1.6.2] at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:736)[12:org.apache.aries.blueprint.core:1.6.2] ... 40 more

I've tried embedding bundles, dynamic-imports, etc... Any suggestions would be greatly appreciated.

Thanks.


Solution

  • Loading a ScriptEngine via the ScriptEngineManager can be complicated because the ClassLoader used by ScriptEngineManager may not be the one you want.

    You can try instantiating Renjin directly:

    RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
    RenjinScriptEngine engine = factory.getScriptEngine();
    

    This might also give you more details if there is actually an error encountered when loading Renjin.