LuaJava
A Script Tool for Java

LuaJava was tested with Windows and Linux platafforms using Java JDK 1.4. LuaJava is JDK 1.4 or higher compatible.

Compiling

LuaJava source distribution includes a Windows nmakefile and a Linux makefile. Edit the config file to point to your JDK, Lua directories and C libraries and compilers.

Windows

LuaJava can be compiled for Windows using nmake from MSVC++. To do that, run the VCVARS32.BAT command before running nmake:

c:\luajava-1.1>"drive:\complete_path_to\VCVARS32.bat"
c:\luajava-1.1>nmake -f nmakefile

Linux

To compile LuaJava for Linux and OSX just do:

make

Installing

LuaJava compilation generates two files: luajava-1.1.jar and luajava-1.1.dll (or libluajava-1.1.so in Unix, or libluajava-1.1.jnilib in MacOSX).

luajava-1.1.jar
This file must be copied to a path in the java application CLASSPATH.
luajava-1.1.dll (or libluajava-1.1.so or libluajava-1.1.jnilib)
This file must be copied to a path in your system path, that depends on your OS. Windows users can place it in the JRE bin directory, or the windows system folder. Unix users can place it in the JRE bin directory or a directory pointed by LD_LIBRARY_PATH environment variable.

Running the LuaJava Console

LuaJava is distributed with a simple console. To run it type :

c:\luajava-1.1>java -cp "luajava-1.1.jar"
       org.keplerproject.luajava.Console

Lua Reference

One of the goals of LuaJava is to allow the programmer to manipulate Java objects in the same way as it manipulates native (Lua) objects. Lua, like most interpreted languages, is dynamically typed. Variables have no type. Instead, each value carries its own type with it. Lua has no declarations, instead variables may contain any value of the language. LuaJava creates a library in Lua called luajava. This library offers 5 functions:

newInstance(className, ...)

This function creates a new Java object, and returns a Lua object that is a reference to the actual Java object. You can access this object with the regular syntax used to access object oriented functions in Lua objects.

The first parameter is the name of the class to be instantiated. The other parameters are passed to the Java Class constructor.

Example:

obj = luajava.newInstance("java.lang.Object")
-- obj is now a reference to the new object
-- created and any of its methods can be accessed.

-- this creates a string tokenizer to the "a,b,c,d"
-- string using "," as the token separator.
strTk = luajava.newInstance("java.util.StringTokenizer", 
    "a,b,c,d", ",")
while strTk:hasMoreTokens() do
    print(strTk:nextToken())
end

The code above should print the following on the screen:

a
b
c
d
bindClass(className)

This function retrieves a Java class corresponding to className. The returned object can be used to access static fields and methods of the corresponding class.

Example:

sys = luajava.bindClass("java.lang.System")
print ( sys:currentTimeMillis() )

-- this prints the time returned by the function.
new(javaClass)

This function receives a java.lang.Class and returns a new instance of this class.

new works just like newInstance, but the first argument is an instance of the class.

Example:

str = luajava.bindClass("java.lang.String")
strInstance = luajava.new(str)
createProxy(interfaceNames, luaObject)

We can also, instead of creating a Java object to be manipulated by Lua, create a Lua object that will be manipulated by Java. We can do that in LuaJava by creating a proxy to that object. This is done by the createProxy function.

The function createProxy returns a java Object reference that can be used as an implementation of the given interface.

createProxy receives a string that contain the names of the interfaces to be implemented, separated by a comma(,), and a lua object that is the interface implementation.

Example:

button = luajava.newInstance("java.awt.Button", "execute")
button_cb = {}
function button_cb.actionPerformed(ev)
 . . .
end

buttonProxy = luajava.createProxy("java.awt.ActionListener", 
    button_cb)

button:addActionListener(buttonProxy)

We can use Lua scripts to write implementations only for Java interfaces.

loadLib(className, methodName)

loadLib is a function that has a use similar to Lua's loadlib function. The purpose of this function is to allow users to write libraries in Java and then load them into Lua.

What loadLib does is call a static function in a given class and execute a given method, which should receive LuaState as parameter. If this function returns a integer, LuaJava takes it as the number of parameters returned by the the function, otherwise nothing is returned.

The following Lua example can access the global eg created by the Java class test.LoadLibExample:

luajava.loadLib("test.LoadLibExample", "open")
eg.example(3)

And this Java example implements the method example:

public static int open(LuaState L) throws LuaException
{
  L.newTable();
  L.pushValue(-1);
  L.setGlobal("eg");

  L.pushString("example");

  L.pushJavaFunction(new JavaFunction(L) {
    /**
     * Example for loadLib.
     * Prints the time and the first parameter, if any.
     */
    public int execute() throws LuaException
    {
      System.out.println(new Date().toString());
    
      if (L.getTop() > 1)
      {
        System.out.println(getParam(2));
      }

      return 0;
    }
  });

  L.setTable(-3);

  return 1;
}

Java Reference

The LuaJava Java Reference Manual is generated by JavaDoc for easier browsing.

Valid XHTML 1.0!

$Id: manual.html,v 1.20 2007/01/23 22:37:28 thiago Exp $