There is something in the LuaJ Readme found here that says the following: 调用require()时,它将首先尝试将模块加载为实现LuaFunction的Java类. When require() is called, it will first attempt to load the module as a Java class that implements LuaFunction.并在发行说明"部分下:and under the Release Notes section: 3.0-alpha2 通过require()加载时,将环境作为LibFunction的第二个参数 3.0-alpha2 Supply environment as second argument to LibFunction when loading via require()我强烈怀疑这与它有关,因为它是在3.0-alpha2版本中添加的,所以我下载了3.0-alpha1版本(正在使用3.0-alpha2),希望它能正常工作,但没有成功.I was strongly suspecting that it has something to do with this since it was added in version 3.0-alpha2, so I downloaded version 3.0-alpha1 (was using 3.0-alpha2), expecting it to work, but it did not.推荐答案与LuaJ的创建者进行了一些交谈之后,我们确定问题出在从3.0-alpha1版本开始的更改中,该版本在lua的程序包中进行了更改通过 require 加载脚本时,.path 被忽略.这意味着require仅会在路径"中查找.搜索脚本时.如果有从子目录中调用的脚本,则将其命名为"place",然后 require 可以通过使用点运算符加载这些脚本来找到这些脚本:After some conversations with the creator of LuaJ, we determined that the problem came from a change that was made starting in version 3.0-alpha1 where lua's package.path was being ignored when loading a script via require. What this means is that require will only look in the path "." when searching for scripts. If there are scripts called from a sub-directory, call it "place" then require could find those scripts by loading them with the dot operator:require( "place.thingy" );我怀疑这个 package.path 问题的数量在社区中有点稀缺,是因为有一种方法可以设置从Java方面起作用的路径在LuaJ v3.0的早期版本中. (一旦我确定了正确的方法,我将发布更新的正确方法,因为我对该过程尚不清楚.)The reason, I suspect, that the number of issues with this package.path issue have been somewhat scarce in the community is because there is a way to set the path from the Java side that works in previous releases of LuaJ v3.0. (I will post an update on the proper way to do this once I have figured it out, as I am still unclear on the process.)总的来说,总之是应该在不久的将来就应该有一个LuaJ v3.0-alpha3,它可以从lua设置 package.path .The long and short of the whole situation is that there should be a LuaJ v3.0-alpha3 on the way soon which allows the package.path to be set from lua.再次感谢吉姆·罗斯伯勒(Jim Roseborough)与我一起解决问题.Thanks again to Jim Roseborough for working with me to resolve the issue.吉姆·罗斯伯勒(Jim Roseborough)的注释:正如内森(Nathan)所言,这确实是luaj-3.0-alpha2及之前版本中的错误.此问题已修复,可以在luaj-3.0-beta1及更高版本中按预期工作,现在可以使用.Note from Jim Roseborough: As Nathan mentioned, this was indeed a bug in luaj-3.0-alpha2 and before. This has been fixed and should work as expected in luaj-3.0-beta1 and later which is now available. 这篇关于LuaJ:无法在Lua脚本中调用"require"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!