Menu

java.lang.ClassNotFoundException

Help
gmeqiminfo
2013-09-04
2016-02-29
  • gmeqiminfo

    gmeqiminfo - 2013-09-04

    Hello !

    We are look for to use Luaj in our production environments based on tomcat and jboss.
    Our POC is this :
    We call a lua script in a web application in java 1.6
    We used ScriptEngineManager, ScriptEngine to process lua script
    In our example, we used commons-lang.jar library added like Web App Librairies (in eclipse)
    The basic script which doesn't work is :

    String script = "UTILString = luajava.bindClass(\"org.apache.commons.lang.StringUtils\")\n" +
    "print( 'UTILString', type(UTILString), UTILString )\n";

    When we execute this code in ParserLua.java:

    try {
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine e = sem.getEngineByName( "luaj" );
    ScriptEngineFactory f = e.getFactory();
    Bindings scriptBindings = new SimpleBindings();
    CompiledScript cs = ((Compilable) e).compile( this.script );
    cs.eval( scriptBindings );
    } catch (ScriptException e) {
    e.printStackTrace();
    } catch (LuaError e) {
    e.printStackTrace();
    }

    this code throw this exception:
    org.luaj.vm2.LuaError: myscript:4 vm error: java.lang.ClassNotFoundException: ch.hcuge.deja.util.UTILString
    at org.luaj.vm2.lib.jse.LuajavaLib.invoke(Unknown Source)
    at org.luaj.vm2.lib.VarArgFunction.call(Unknown Source)
    at org.luaj.vm2.LuaClosure.execute(Unknown Source)
    at org.luaj.vm2.LuaClosure.call(Unknown Source)

    the probleme seems to comme from LuajavaLib class :


    case BINDCLASS: {
    final Class clazz = classForName(args.checkjstring(1));
    return JavaClass.forClass(clazz);
    }


    protected Class classForName(String name) throws ClassNotFoundException {
    return Class.forName(name, true, ClassLoader.getSystemClassLoader());
    }

    => just the SystemClassLoader is used, and commons-lang is not in !

    test in my program before executing cs.eval( scriptBindings ) :

    Class c = Class.forName("org.apache.commons.lang.StringUtils", true, ClassLoader.getSystemClassLoader());
    => throw java.lang.ClassNotFoundException

    Class c = Class.forName("org.apache.commons.lang.StringUtils", true, this.getClass().getClassLoader());
    =>ok

    And so, all classes in our framwork lib don't work in a lua script
    dao = luajava.newInstance( "ch.xxx.yyy.zzz.dao.DAODocument"); =>throw java.lang.ClassNotFoundException

    How can we load from the lua script, user java libraries in a web application?

    Thanks for your help.

     
  • gmeqiminfo

    gmeqiminfo - 2016-02-29

    to help someone :

    we modifiy LuajavaLib class to add web-app class loader

    // load classes using app loader to allow luaj to be used as an extension
    protected Class<?> classForName(String name) throws ClassNotFoundException{
    
        Class<?> c = null;
        try {
            c = Class.forName(name, true, this.getClass().getClassLoader());
        } catch (ClassNotFoundException e) {
            log.error("Attempt to get [" + name + "] from classloader");
        }
        return c;
    }
    
     

    Last edit: gmeqiminfo 2016-02-29

Log in to post a comment.