java - LuaJ(Java Lua库): Calling Lua functions in other files from a Lua file

标签 java lua luaj luajava

To begin, I'm aware of this question, but I don't think it quite fits what I'm doing. Either way, the answer is a bit confusing my opinion. I'd like to find an answer for my problem that's more specific to what I'm doing.

这里的目标是让lua文件chatterToolsTest成功地将“Test success”打印到控制台。不幸的是,我目前的方法不太有效。有人可以帮忙吗?我不是最擅长 Lua,所以在这种情况下我的 Lua 代码可能是错误的。请查看下面的片段。

另一个限制:我无法启用从 java 端使用模块。两个 Lua 文件之间的任何引用都只能通过 Lua 获取。这是因为我正在为 Java 项目开发一个修改系统,并且需要 Lua 能够在 Java 端进行最小的更改。

请记住,我没有将 Lua 文件存储在 JAR 文件或任何包中,它们包含在 Java 程序根工作目录的文件夹中,就像资源文件夹一样。

chatterToolsTest.lua:

function main()
  print("Test start.");

  local test = require("chatterTools");
  chatterTools:test();
end

chatterTools.luachatterToolsTest.lua调用的类:

function test()
  print("Test success");
end

这两个文件都位于名为 world/NOKORIWARE/lua/ 的文件夹中:

最后,这是使用 LuaJ 调用它们的 Java 测试类:

public class LuaTest {
    public static void main(String args[]) {
        new LuaTest().run("NOKORIWARE/lua/chatterToolsTest.lua", "main");
    }

    private Globals buildGlobals() {
        Globals globals = new Globals();

        globals.load(new JseBaseLib());
        globals.load(new PackageLib());
        globals.load(new Bit32Lib());
        globals.load(new TableLib());
        globals.load(new StringLib());
        globals.load(new JseMathLib());
        globals.load(new WhitelistedLuajavaLib());

        LoadState.install(globals);
        LuaC.install(globals);

        return globals;
    }

    /**
     * Runs the given lua file. It must be relative to the lua path.
     */
    private void run(String luaPath, String functionName, Object... arguments) {
        LuaValue[] coercedValues = null;

        if (arguments != null) {
            //Coerce arguments into LuaValues
            coercedValues = new LuaValue[arguments.length];

            for (int i = 0; i < arguments.length; i++) {
                coercedValues[i] = CoerceJavaToLua.coerce(arguments[i]);
            }
        }

        //Configure lua file
        Globals globals = buildGlobals();
        globals.get("dofile").call(LuaValue.valueOf("./world/" + luaPath));

        //Call the passed-in function of the lua file.
        try {
            LuaValue call = globals.get(functionName);
            if (arguments != null) {
                call.invoke(coercedValues);
            }else {
                call.invoke();
            }
        } catch (Exception e) {
            e.printStackTrace();
            TinyFileDialog.showMessageDialog("Caught " + e.getClass().getName(), e.getMessage(), TinyFileDialog.Icon.INFORMATION);
        }
    }
}

这是我运行 Java 程序时打印的错误:

org.luaj.vm2.LuaError: @./world/NOKORIWARE/lua/chatterToolsTest.lua:4 module 'chatterTools' not found: chatterTools
    no field package.preload['chatterTools']
    chatterTools.lua
    no class 'chatterTools'
    at org.luaj.vm2.LuaValue.error(Unknown Source)
    at org.luaj.vm2.lib.PackageLib$require.call(Unknown Source)
    at org.luaj.vm2.LuaClosure.execute(Unknown Source)
    at org.luaj.vm2.LuaClosure.onInvoke(Unknown Source)
    at org.luaj.vm2.LuaClosure.invoke(Unknown Source)
    at org.luaj.vm2.LuaValue.invoke(Unknown Source)
    at nokori.robotfarm.test.LuaTest.run(LuaTest.java:64)
    at nokori.robotfarm.test.LuaTest.main(LuaTest.java:21)

感谢任何帮助或相关资源的链接。

最佳答案

默认的LuaJ工作目录与Java的相同。一旦我弄清楚了这一点,我就能够正确使用 require() .

chatterTools.lua改为:

local chatterTools = {}

function chatterTools.test()
  print("Test success");
end

return chatterTools;

最后chatterToolsTest.lua必须这样改变:

function main()
  print(package.path);

  local chatterTools = require("world.NOKORIWARE.lua.chatterTools");
  chatterTools:test();
end

Lua 处理像上面这样的包,所以而不是 world/NOKORIWARE/lua/chatterTools.lua它变成了您在 require() 中看到的内容打电话。

完成这些更改后,我运行程序并得到以下结果:

?.lua
Test success

考虑到所有这些,这个解决方案比我在本问题开头链接的问题中的答案要直接得多。希望这会对你们中的一些人有所帮助。

要详细了解我是如何解决这个问题的,请查看以下资源:

how to call function between 2 .lua

https://forums.coronalabs.com/topic/38127-how-to-call-a-function-from-another-lua-file/

关于java - LuaJ(Java Lua库): Calling Lua functions in other files from a Lua file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319082/

相关文章:

java - 将属性文件添加到 IntelliJ 的类路径

java - java中下载多个文件

c++ - 编译Lua 5.3 Mingw64 MSys2

LuaJ:无法在 Lua 脚本中调用 'require' 函数

java - 找不到符号(调用方法)

java - 打包到 Apache Tomcat 的 WAR 时配置 Web 资源的位置

function - Lua 如何创建可用于变量的自定义函数?

svg - 在 Lua 中解析 SVG 路径定义 ("d")

java - 如何实现LuaJ暂停?

java - 如何用CGLib调用原始方法?