Skip to content

gudzpoz/luajava

Repository files navigation

LuaJava

Build Status License: MIT Codecov Java 8 Sonatype Nexus (Snapshots) Maven Central

Build Status Document Version

Tests: macOS on M1 Tests: Linux on arm64

Hello World Example

About

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.

Platforms and Versions

Lua 5.1 Lua 5.2 Lua 5.3 Lua 5.4 LuaJIT LuaJ
5.1.5 5.2.4 5.3.6 5.4.6 ae4735f LuaJ fork

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.

Artifacts

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!)

Java module

The java module provides these functions:

  • array: Create a Java array.
  • caught: Return the latest captured Java Throwable
  • detach: Detach the sub-thread from registry to allow for GC
  • import: Import classes from Java
  • loadlib: Load a Java method, similar to package.loadlib
  • luaify: Convert Java values to Lua values
  • method: Provide an alternative way to call Java methods
  • new: Construct a Java object
  • proxy: Create a Java object, calls to whose method will be proxied to Lua functions
  • unwrap: Return the backing table of a proxy object

Examples

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()

More

Check out AWT Example for a more complex example.

You may also have a look at our tests.

Credits

License

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.