This is yet another fork of the original LuaJava.
LuaJava is a scripting tool for Java. The goal of this tool is to allow scripts written in Lua to manipulate components developed in Java. LuaJava allows Java components to be accessed from Lua using the same syntax that is used for accessing Lua's native objects, without any need for declarations or any kind of preprocessing.
LuaJava also allows any Java interface to get implemented in Lua and passed as parameter to any method, and when called, the equivalent function will be called in Lua, the result passed back to Java.
LuaJava is available under the same license as Lua 5.1, that is, it can be used at no cost for both academic and commercial purposes.
Documentation is available at LuaJava along with Javadoc. You are also recommended to familiarize yourself with the Lua C API since this library is more or less just a thin wrapper and requires some basic understanding of the C API.
Supported platforms: Windows, Linux, MacOS and Android. Compiled against both ARM and x32/x64. Binaries are not yet tested for iOS.
Compiled natives are available for most common platforms. Check out LuaJava Platforms for a platform matrix. LuaJ bindings do not need native binaries and should run on all platforms theoretically.
To include LuaJava into your project, you need to include two artifacts, one for the Java bindings, the other for the compiled native binaries. (For LuaJ bindings, you don't need the latter one. However, you will need to add JitPack to your repositories.)
// Example: LuaJIT with Desktop natives
implementation 'party.iroiro.luajava:luajit:4.0.2'
runtimeOnly 'party.iroiro.luajava:luajit-platform:4.0.2:natives-desktop'
Different artifacts are provided for different Lua versions and different platforms. Check out Getting Started for an overview. Or you may also search in the Maven Central.
Optionally, you may include party.iroiro.luajava:jsr223
to provide JSR 223 functionalities. (Note that you still need the above artifacts!)
The java
module provides these functions:
array
: Create a Java array.caught
: Return the latest captured JavaThrowable
detach
: Detach the sub-thread from registry to allow for GCimport
: Import classes from Javaloadlib
: Load a Java method, similar topackage.loadlib
luaify
: Convert Java values to Lua valuesmethod
: Provide an alternative way to call Java methodsnew
: Construct a Java objectproxy
: Create a Java object, calls to whose method will be proxied to Lua functionsunwrap
: Return the backing table of a proxy object
Here is a simple example on how to correctly initialize new Lua instance.
This example will push message
variable to Lua with value Hello World from Lua
,
and then print it using the Java println
.
public static void main(String[] args) {
try (Lua L = new Lua51()) {
L.set("message", "Hello World from LuaJava");
L.run("java.import('java.lang.System').out:println(message)");
}
}
And a more advanced "Hello World":
print = java.method(java.import('java.lang.System').out,'println','java.lang.Object')
Ansi = java.import('org.fusesource.jansi.Ansi')
thread = java.import('java.lang.Thread')(function()
print(Ansi:ansi():render('@|magenta,bold Hello |@'):toString())
end)
thread:start()
Check out AWT Example for a more complex example.
You may also have a look at our tests.
Code under ./example is licensed under GNU General Public License Version 3.
Other parts are licensed under the MIT license. The project includes code from other projects, whose licenses may be found at ./LICENSE.