javalualuajava

No console output when running LuaJava Hello, World program


I'm starting out to learn Lua script usage in Java via LuaJava; my IDE is Eclipse.

But when I execute this simple Hello World snippet there is no output in the Eclipse console.

Took the code snippet from here

package com.cpg.lua;

import org.keplerproject.luajava.LuaState;
import org.keplerproject.luajava.LuaStateFactory;

public class Hello
{
public static void main(String[] args)
{ 
  LuaState luaState;
  luaState = LuaStateFactory.newLuaState();
  luaState.openLibs();
  luaState.LdoFile("hello.lua");
  luaState.close();
}
}

hello.lua

function hello()
    print("Hello World from Lua!") 
end

hello()

But the script beneath works perfectly well.

hello2.lua

print("Hello World from Lua!") 

Anyone know why the script with the function definition inside does nothing when called from Java but when executed through the console works perfectly?


Solution

  • I haven't tried or seen a function called like that. But you can call the hello() function from the Java like this:

    LuaState l = LuaStateFactory.newLuaState();
    l.doFile("main.lua");
    l.getGlobal("hello");
    l.call(0, 0);