Console output missing when running LuaJava Hello, World program

I am starting 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 a 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 below works fine.

hello2.lua

print("Hello World from Lua!") 

      

Does anyone know why a script with a function definition inside does nothing when called from Java, but works fine when executed through the console?

+3


source to share


1 answer


I haven't tried or seen a function called like this. But you can call a function hello()

from Java like this:



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

      

+2


source







All Articles