0

我正在使用 LuaJ 从 Java 运行 Lua 脚本。在类路径中加载 Lua 文件可以正常工作,但是当从不同的 Lua 文件中导入其中一个文件时,它会说该文件不存在。我的 Lua 文件非常基础:

-- Both files are inside the .jar file at these locations
-- MyProgram.jar/
--   com/my/program/
--     FirstFile.lua
--     OtherFile.lua

-- FirstFile.lua
local thing = require("com.my.program.OtherFile");
print(thing);

-- OtherFile.lua
return 42;

Mypackage.path设置为?.lua(LuaJ 中的默认值)。这就是我从 Java 加载文件的方式:

Globals _G = JsePlatform.standardGlobals();
final Reader scriptFile = new InputStreamReader(Program.class.getResourceAsStream("FirstFile.lua"));
LuaValue script = _G.loadFile(scriptFile, "FirstFile.lua");
script.call(); // Run it

这会找到该文件,但在执行期间它会因以下错误而崩溃:

module 'com.my.program.OtherFile' not found
    no field package.preload['com.my.program.OtherFile'] // Expected
    com\my\program\OtherFile.lua // This is strange, since the file definitely exists (I can run it from Java).
    no class 'com.my.program.OtherFile' // Expected

我难住了。我知道我之前已经让它工作了,但我不记得我是怎么做到的。

4

0 回答 0