How to embed Lua codes in Java?

LuaServer Side ProgrammingProgramming

Lua is probably the most commonly used embedding language that can be integrated or embedded into different major programming languages. There are different projects that does this work of embedding for us, Lua has been embedded into C, C# and Java also.

In this article, we will explore how the embedding of Lua in Java works, and we will also explore the most commonly used project that does this for us.

Embedding Lua with Java simply means the fact we should be able to run the code inside the Lua file with the help of the java commands. Just like how we run a normal Java file with the following command

javac HelloWorld.java
java HelloWorld
or
java HelloWorld

We should be able to replace the .java file with the .lua one.

The most commonly used project for embedding Lua in Java is luaj. The source code is present on this link.

You can clone the repository on your local machine with the help of the following command −

git clone https://github.com/luaj/luaj.git

After cloning, you will be able to find several examples present inside the examples/ directory.

Inside that directory a folder named lua is present and we will use one example from that which is the hello.lua one.

Just type the following command in the terminal −

java -cp luaj-jse-3.0.2.jar lua examples/lua/hello.lua

Output

hello, world

This is how easy it is to embed Lua in Java.

raja
Published on 19-Jul-2021 12:23:44
Advertisements