Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:28:18+00:00 2026-06-13T10:28:18+00:00

I am trying to build a controller using Luaj + java. I have the

  • 0

I am trying to build a controller using Luaj + java. I have the following java classes

public class Duck {
  public void talk() { System.out.println("Duck quacks!"); }
  public void walk() { System.out.println("Duck walks!"); }
}

public class Person {
  public void talk() { System.out.println("Person talks!"); }
  public void walk() { System.out.println("Person walks!"); }
}

and the following lua script for the controller:

onTalk(obj) 
  obj:talk();
end

onWalk(obj)
   obj:walk();
end

I would ideally like to define one controller (written in lua) where I will keep all of the program’s logic, and I would like to expose API from that controller to my java code. I was trying to use the following approach:

ScriptEngineManager sem     = new ScriptEngineManager();
ScriptEngine        engine  = sem.getEngineByExtension(".lua");
ScriptEngineFactory factory = engine.getFactory();

// Script defined above
CompiledScript cs = ((Compilable)engine).compile(MY_LUA_SCRIPT);
SimpleBindings b = new SimpleBindings();

b = newSimpletBindings();

LuaValue onWalkHandler = (LuaValue)b.get("onWalk");
//func.call(LuaValue.valueOf(duck)); // Passing duck object does not work ???

I am not able to pass the object to the LuaValue. How can I pass a java object to the lua script?

PS: In general, when using Java and embedded scripting, do people bundle functions in one script, or is there a separate script for every callback?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:28:19+00:00Added an answer on June 13, 2026 at 10:28 am

    I was searching luaj entries and found your unanswered. Your question was interesting and made me search. Then I realized that it was asked 2 years ago… I hope my answer can be useful to somebody else! Code based on luaj-3.0-alpha1.

    We need a valid Lua script (you forgot the function keyword):

    function onTalk(javaObj)
      print(type(javaObj) .. " " .. tostring(javaObj))
      print(javaObj.name)
      javaObj:talk()
      return true
    end
    
    function onWalk(javaObj)
      javaObj:walk()
      return 1, "km"
    end
    

    I added a bit of trace…
    I also made classes similar to your:

    class Dog
    {
      public String name;
      Dog(String n) { name = n; }
      public void talk() { System.out.println("Dog " + name + " barks!"); }
      public void walk() { System.out.println("Dog " + name + " walks..."); }
    }
    class Cat
    {
      String name;
      Cat(String n) { name = n; }
      public void talk() { System.out.println("Cat " + name + " meows!"); }
      public void walk() { System.out.println("Cat " + name + " walks..."); }
    }
    

    Adding a field to test this too. For my test, I just declared the classes inside the method creating their instances:

    Dog dog = new Dog("Rex");
    Cat cat = new Cat("Felix");
    

    I first tried to convert these Java objects to Lua, using LuaValue luaDog = LuaValue.userdataOf(dog); but it doesn’t work: we indeed have userdata, as shown by the traces, but no metatable, so we cannot call the methods nor access the fields.

    After searching a lot, I found out the right incantation:

    CompiledScript script = ((Compilable) scriptEngine).compile(reader);
    Bindings sb = new SimpleBindings();
    script.eval(sb); // Put the Lua functions into the sb environment
    LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua
    LuaFunction onTalk = (LuaFunction) sb.get("onTalk"); // Get Lua function
    LuaValue b = onTalk.call(luaDog); // Call the function
    System.out.println("onTalk answered: " + b);
    LuaFunction onWalk = (LuaFunction) sb.get("onWalk");
    LuaValue[] dogs = { luaDog };
    Varargs dist = onWalk.invoke(LuaValue.varargsOf(dogs)); // Alternative
    System.out.println("onWalk returned: " + dist);
    

    I appreciate the Luaj API… 🙂 Probably more made for Java programmers while other libraries seem to aim more at Lua / C programmers…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a program using model-view-controller. Specifically: I have a state
I am trying to build a football game using rails but have difficulty in
I'm trying to build an admin page using the pages controller and a admin_index()
I have an MVC3 razor project, which I am trying to build using multiple
I have an error in MVC/Controllers/ProductCOntroller.cs when trying to build solution. Need to have
Trying to build sslsniff on a RHEL 5.2 system here. When compiling sslsniff on
I am trying to build an API using Rails that will inspect the request
I am trying to build a sample for myself using MVC3, Razor view engine
I'm trying to build a guestbook module within a Rails site using the Alchemy
Basically i'm trying to build a comment system. The user looks at a photo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.