Lua is a useful scripting language, relatively small in comparison to Python, but it's not as good as it is, especially when it comes to game development (and WOW uses Lua as a script). Lua in the implementation of the c\c++ I will not say, a random search, everywhere is the introduction of this, I want to say is how to use LUA in Java to improve programming efficiency, enhance your program extensibility.
First, to use LUA scripts on Java, there must be a related API for the LUA scripting interpreter and Java programs to access these scripts, the related class library. I am using an open source project called Luajava, which can be found in: http://www.keplerproject.org/luajava/ Luajava class Library and source code, using document funding
Download and unzip to include two files (i downloaded version 1.1): Luajava-1.1.jar file and Luajava-1.1.dll Dynamic Connection library file
Luajava-1.1.jar is easy, that is, adding it to your project's classpath so that the program can use the API it provides
Luajava-1.1.dll is in trouble, you have to add it to your Windows installation directory, such as you use XP, installed on the C drive, then directly add it to the C:\WINDOWS directory can, of course, you can also add it under the JRE under your JDK
Well, now your project can use LUA scripts to implement the dynamic extension feature! But don't worry, you have to have tools to write Lua scripts, right? It's impossible to write with Notepad, right???????
You can use UltraEdit, but after you open the Lua file with your UE, you will find that it is similar to Notepad and not highlighted (possibly a new version of the Lua script is supported), if your UE does not support it, then go to UltraEdit's official website to download the WORDFiles file that supports LUA ( Http://www.ultraedit.com/files/wf/lua.txt), is a text file (Lua.txt). Open the UltraEdit installation directory wordfile.txt, the Lua.txt file content copy paste to the end of the Wordfile.txt, save, OK, so ultraedit syntax highlighting more than a LUA, you can choose to use. Syntax highlighting support in other languages is similar to this.
But if you were a Java developer, would you have used eclipse? Can I write a LUA script directly under Eclipse? The answer is YES!
This is certainly the powerful plug-in management feature of Eclipse, you can download the Luaeclipse plugin to make your eclipse have the ability to write LUA scripts (you can highlight your script, isn't it cool), you can download here:/http www.ideais.com.br/luaeclipse/
After the download is installed, your eclipse can create and write Lua scripts, and note that the properties of the LUA in the preferences are set up (Eclipse install plugin I don't have to say it???? )
OK, now everything is ready, let's have a HelloWorld!
First create a Testlua project in Eclipse and then write the following program:
Import org.keplerproject.luajava.*;
public class Hello
{
public static void Main (string[] args)
{
Luastate L = Luastatefactory.newluastate ();
L.openlibs ();
System.out.println ("Here is the Java program called Lua Script");
Load the script Hello.lua and execute
L.ldofile ("Res/hello.lua");
}
}
Well, the program is finished, of course, is saved as Hello.java, note, this is the Java code! This is the Java code called a script called Hello.lua, below is the contents of this script file (you can directly copy them into your Hello.lua file):
================================================================
--Basic methods
Print ("You are now using the LUA scripting language")
Print ("Let's feel the magic of it!\n")
--Feature 1, assignment
a={1,2}
B=a
Print (A==b, a~=b)-Output true, False
a={1,2}
b={1,2}
Print (A==b, a~=b)-Output false, True
--Features 2, exchange
a,b=1,2
A,b=b,a
Print (a)
Print (b)
Print ("Connect" ... " String ". 2^3)
Print (Type (2))
--while Cycle
I=0
Print ("While loop example")
While I<5
Do
print (i);
I=i+1
End
--repeat Cycle
I=0
Print ("Repeat loop example")
Repeat
Print (i)
I=i+1
Until I>=5
--for Cycle
Print ("For loop example")
For i=0,5,1
Do
Print (i)
End
t1={}
T1[1] = 10
Print (t1[1])
function Fun (A, b,...)
Print (a)
Print (b)
Print (arg[1])
Print (arg[2])
Print (Arg[3])
Return
End
a,b=2,3
Fun (a,b,200,400,500)
========================================================
Well, the script above is fine if you don't know what it means, just run it.
You can compile and execute the Hello.java program, and you will see the following output:
========================================================
Here is the Java program calling Lua script
You are now using the LUA scripting language
Let's feel the wonders of it together!
True False
False true
2
1
Connection String 8
Number
While Loop example
0
1
2
3
4
Repeat cycle examples
0
1
2
3
4
For Loop example
0
1
2
3
4
5
10
2
3
200
400
500
==========================================================
What, isn't it cool? Think it's not interesting? OK, here's a script:
frame = luajava.newinstance ("Java.awt.Frame", "Lua java Console")
console = Luajava.newinstance ("Java.awt.TextArea")
BUTTONS_PN = Luajava.newinstance ("Java.awt.Panel")
EXECUTE_BT = Luajava.newinstance ("Java.awt.Button", "Execute")
CLEAR_BT = Luajava.newinstance ("Java.awt.Button", "Clear")
EXIT_BT = Luajava.newinstance ("Java.awt.Button", "Exit")
Frame:setsize (600,300)
Buttons_pn:add (EXECUTE_BT)
Buttons_pn:add (CLEAR_BT)
Buttons_pn:add (EXIT_BT)
BorderLayout = Luajava.bindclass ("Java.awt.BorderLayout")
Frame:add (Borderlayout.north, console)
Frame:add (Borderlayout.south, BUTTONS_PN)
Frame:pack ()
Frame:show ()
--
--Listeners
--
EXECUTE_CB = {
actionperformed = function (EV)
Print ("Execute")
Pcall (LoadString (Console:gettext ()))
End
}
Jproxy = Luajava.createproxy ("Java.awt.event.ActionListener", EXECUTE_CB)
Execute_bt:addactionlistener (Jproxy)
CLEAR_CB = {actionperformed= function (EV)
Print ("clear");
Console:settext ("");
End
}
Jproxy = Luajava.createproxy ("Java.awt.event.ActionListener", CLEAR_CB)
Clear_bt:addactionlistener (Jproxy)
EXIT_CB = {actionperformed=function (EV)
Print ("Exit")
Frame:setvisible (False)
Frame:dispose ()
End
}
Jproxyb = Luajava.createproxy ("Java.awt.event.ActionListener", EXIT_CB)
Exit_bt:addactionlistener (JPROXYB)
CLOSE_CB = {}
function close_cb.windowclosing (EV)
Print ("Close")
Frame:setvisible (False)
Frame:dispose ()
End
function close_cb.windowactivated (EV)
Print ("act")
End
Jproxy = Luajava.createproxy ("Java.awt.event.WindowListener", CLOSE_CB)
Frame:addwindowlistener (Jproxy)
From: http://hi.baidu.com/panzhong/blog/item/e991b30a81d5da3fb1351d7b.html
Http://www.cnblogs.com/thingsoft/archive/2011/04/09/2010227.html
Using LUA scripting language in Java (GO)