所以,我有一个类似的脚本:
function testfunction()
print("Test from testfunction");
end
Run Code Online (Sandbox Code Playgroud)
我可以从Lua调用Java函数,但是如何完成相反的操作呢?如何使用LuaJ从Java调用Lua函数?
小智 12
我正在四处寻找解决同样的问题,我想出了一些与Profetylen的答案非常相似的东西
test.java:
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
public class test
{
public static void main(String[] args)
{
//run the lua script defining your function
LuaValue _G = JsePlatform.standardGlobals();
_G.get("dofile").call( LuaValue.valueOf("./test.lua"));
//call the function MyAdd with two parameters 5, and 5
LuaValue MyAdd = _G.get("MyAdd");
LuaValue retvals = MyAdd.call(LuaValue.valueOf(5), LuaValue.valueOf(5));
//print out the result from the lua function
System.out.println(retvals.tojstring(1));
}
}
Run Code Online (Sandbox Code Playgroud)
test.lua:
function MyAdd( num1, num2 )
return num1 + num2
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9177 次 |
| 最近记录: |