LuaJ is fairly flexible in respects to how you can go about things such as importing classes from java.
You've not specified how you have done this so I'm going to assume you've gone about it by creating a library of Java Functions as a bridge to build a module that you can require (with require"moduleName"
) like so:
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;
/*
* When require"module name" is called in luaJ it also searches for java classes with a classname that matches
* the given module name that inherit from LuaFunction and have a default constructor, it then
* calls the method call(LuaValue moduleName, LuaValue globalEnviroment) passing the module name string and
* global environment as LuaValues and returning the return value of this call as it's own.
*
*/
public class Example extends TwoArgFunction{
@Override
public LuaValue call(LuaValue modname, LuaValue globalEnv) {
// Creates a table to place all our functions in
LuaValue table = LuaValue.tableOf();
// Sets the value 'name' in our table to example. This is used in the example function
table.set("name", "example");
// Sets the value 'exampleMethod' in our table to the class of type OneArgFunction
// we created below
table.set("exampleMethod", exampleMethod);
//Finally returns our table. This value is then returned by require.
return table;
}
/* Creates a function that takes one arg, self.
This emulates the creation of a method in lua and can be called,
when added to a table, as table:exampleMethod().
This is possible as in Lua the colon in functions is
Syntactic suger for object.function(object)
*/
OneArgFunction exampleMethod = new OneArgFunction() {
@Override
public LuaValue call(LuaValue self) {
if(self.istable()) {
return self.get("name");
}
return NIL;
}
};
}
This can then be used in your Lua code as follows:
--Imports the class Wrapper we created
example = require"Example"
--Prints the return value of calling exampleMethod
print("The name of the class is: "..example:exampleMethod())
--And then used as an index like so
example2 = setmetatable({},{__index=example})
example2.name = "example2"
print("The name of the class is: "..example2:exampleMethod())
As I stated at the top of the answer, LuaJ is flexible in respects to how you can go about these things and this is only the way I would do it.
Due to my reputation I was unable to post a comment asking how you've gone about importing
Java classes in LuaJ, feel free to clarify in the comment on this answer.