Bcel version problem when using LuaJC in multi Thread #85

Closed
opened 2021-03-25 08:50:28 +00:00 by lewy95 · 1 comment
lewy95 commented 2021-03-25 08:50:28 +00:00 (Migrated from github.com)

I tried to use LuaJC in the situation of multi thread but got failed.

Problems

I used the official case referring to http://www.luaj.org/luaj/3.0/examples/jse/SampleMultiThreaded.java, and I just added one line code 'LuaJC.install(g);' after the initialization of Globals,just like the code below.

public void run() {
    try {
        // Each thread must have its own Globals.
        Globals g = JsePlatform.standardGlobals();
        LuaJC.install(g);
        // Once a Globals is created, it can and should be reused
        // within the same thread.
        g.loadfile(script1).call();
        // g.loadfile(script2).call();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But the result is always wrong with Exception:

Exception in thread "Runner-0" java.lang.ClassFormatError: Invalid length 65535 in LocalVariableTable in class file lua_hello$sayHello at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.lang.ClassLoader.defineClass(ClassLoader.java:642) at org.luaj.vm2.luajc.JavaLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) at java.lang.Class.getConstructor0(Class.java:3075) at java.lang.Class.newInstance(Class.java:412) at org.luaj.vm2.luajc.JavaLoader.load(Unknown Source) at org.luaj.vm2.luajc.JavaLoader.load(Unknown Source) at org.luaj.vm2.luajc.JavaLoader.load(Unknown Source) at org.luaj.vm2.luajc.LuaJC.load(Unknown Source) at org.luaj.vm2.Globals.load(Unknown Source) at org.luaj.vm2.Globals.loadfile(Unknown Source) at cn.xzxy.lewy.luaj.SampleMultiThreaded$Runner.run(SampleMultiThreaded.java:40) at java.lang.Thread.run(Thread.java:748)

So,I gusse there is a problem when bcel complies the same lua file in multi thread situation.

Solutions

After tough attempts,I find two solutions.

  1. move the code into main thread jusy like this
static class Runner implements Runnable {
        final String script;
        LuaValue luaValue;

        Runner(String script) {
            Globals g = JsePlatform.standardGlobals();
            LuaJC.install(g);
            luaValue = g.loadfile(script);
            this.script = script;
        }

        public void run() {
            try {
                luaValue.call();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  1. replace the bcel 5.2 with bcel 6.5.0
<dependency>
    <groupId>org.apache.bcel</groupId>
    <artifactId>bcel</artifactId>
    <version>6.5.0</version>
</dependency>

Conclusion

According to the introduction of official website, the bcel-5.2.jar is the first we know.

java -cp "lib/luaj-jse-3.0.1.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua

But bcel-5.2.jar can only work when we use LuaJC in single thread situation. We should use the version of 6.5.0 to deal with multi thread.

Finally,LuaJC is indeed the better way that java program calls lua script.

I tried to use LuaJC in the situation of multi thread but got failed. ### Problems I used the official case referring to http://www.luaj.org/luaj/3.0/examples/jse/SampleMultiThreaded.java, and I just added one line code 'LuaJC.install(g);' after the initialization of Globals,just like the code below. ``` public void run() { try { // Each thread must have its own Globals. Globals g = JsePlatform.standardGlobals(); LuaJC.install(g); // Once a Globals is created, it can and should be reused // within the same thread. g.loadfile(script1).call(); // g.loadfile(script2).call(); } catch (Exception e) { e.printStackTrace(); } } ``` But the result is always wrong with Exception: `Exception in thread "Runner-0" java.lang.ClassFormatError: Invalid length 65535 in LocalVariableTable in class file lua_hello$sayHello at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.lang.ClassLoader.defineClass(ClassLoader.java:642) at org.luaj.vm2.luajc.JavaLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) at java.lang.Class.getConstructor0(Class.java:3075) at java.lang.Class.newInstance(Class.java:412) at org.luaj.vm2.luajc.JavaLoader.load(Unknown Source) at org.luaj.vm2.luajc.JavaLoader.load(Unknown Source) at org.luaj.vm2.luajc.JavaLoader.load(Unknown Source) at org.luaj.vm2.luajc.LuaJC.load(Unknown Source) at org.luaj.vm2.Globals.load(Unknown Source) at org.luaj.vm2.Globals.loadfile(Unknown Source) at cn.xzxy.lewy.luaj.SampleMultiThreaded$Runner.run(SampleMultiThreaded.java:40) at java.lang.Thread.run(Thread.java:748)` So,I gusse there is a problem when bcel complies the same lua file in multi thread situation. ### Solutions After tough attempts,I find two solutions. 1. move the code into main thread jusy like this ``` static class Runner implements Runnable { final String script; LuaValue luaValue; Runner(String script) { Globals g = JsePlatform.standardGlobals(); LuaJC.install(g); luaValue = g.loadfile(script); this.script = script; } public void run() { try { luaValue.call(); } catch (Exception e) { e.printStackTrace(); } } } ``` 2. replace the bcel 5.2 with bcel 6.5.0 ``` <dependency> <groupId>org.apache.bcel</groupId> <artifactId>bcel</artifactId> <version>6.5.0</version> </dependency> ``` ### Conclusion According to the introduction of official website, the bcel-5.2.jar is the first we know. `java -cp "lib/luaj-jse-3.0.1.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua` But bcel-5.2.jar can only work when we use LuaJC in single thread situation. We should use the version of `6.5.0` to deal with multi thread. Finally,LuaJC is indeed the better way that java program calls lua script.

We updated bcel

We updated bcel
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: open-autonomous-connection/luaj#85