J2lua Script Programming Tutorial

Source: Internet
Author: User
Tags ultraedit

J2luaThe Script Programming learning tutorial is the content to be introduced in this article, mainly to learnJ2EEAboutLuaFor more information, see the detailed description.

1. embedded scripting language suitable for j2-based Systems

The embedded scripts have high requirements for the j2s platform with limited hardware resources.

1) small size, high execution efficiency, and low memory usage.

2) The syntax is simple, easy to learn, and easy to use. It is best to use a popular scripting language ).

3) strong independence and independent from other programs. It is suitable for porting to a KVM environment.

Based on the above conditions for comparison and filtering, I finally chose Lua and ECMAScript.

LuaIs a small scripting language. The author is Brazilian. It is designed to embed applications and provide flexible scalability and customization for applications. Lua is currently recognized as one of the most compact, highly efficient, and powerful scripting languages. It is widely used in game software. The syntax is similar to basic, which is very easy to use.

ECMAScript is a scripting language standardized by the European Computer Manufacturers Association ECMA) through ECMA-262. This language is widely used on the World Wide Web and is often called JavaScript or JScript, but in fact the latter two are extensions of the ECMA-262 Standard

There is also a Java Dynamic Language, Hecl, which is almost designed for the j2s environment. Hecl is an advanced scripting language implemented in Java. It features very small, scalable, flexible, and easy to learn and use.

II. Introduction to open-source projects

1) kahlua (KARUA)
Kahlua is a very small Lua interpreter based on CLDC1.1, which is easy to expand. You only need to work with a Lua compiler to execute the compiled Lua source code.

Project address: http://code.google.com/p/kahlua/

2) mochalua (muha)

Mochalua is also a Lua interpreter based on CLDC1.1.
 
Project address: http://code.google.com/p/mochalua/

3) minijoe

Minijoe claims to be the smallest JavaScript runtime environment and basically supports all the ECMA-262 features. It carries a compiler and an interpreter. This compiler compiles the ECMAScript source code into a set of simplified bytecode designed by the author. The interpreter then executes the bytecode. The entire code is very simple and efficient, especially the part of the interpreter.

Address: http://www.minijoe.com/

4) Hecl

This dynamic language was initially designed for the j2's platform. It comes with a lot of j2's demonstrations. However, unlike the above three projects, it adopts an interpreted execution method, which reduces the execution efficiency of scripts to a certain extent.
Address: http://www.hecl.org/

In short: due to the shortage of resources on the agent platform, most of the embedded solutions adopt the compilation and execution mode. Compile the script source code into a simpler "bytecode", and then execute these "bytecode" on the client ". The advantage of doing so is that the interpreter is simpler and the execution is more efficient. For the open-source projects above, we suggest learning about kahlua, minijoe, and Hecl. Mochalua seems to be updated slowly.

3. Use Lua scripting language in Java

Lua is a practical scripting language. Compared with Python, it is relatively small, but its functions are not inferior. Especially in Game Development, WoW uses Lua as a script ). I will not talk much about the implementation of Lua in C \ C ++. I will search for it on the Internet and introduce it everywhere, what I want to talk about is how to use Lua in Java to improve programming efficiency and enhance program scalability.

First, to use the Lua script in Java, you must have a Lua script interpreter and Java programs that can access the relevant APIs of these scripts, that is, the relevant class library. I am using an open source project called LuaJava, you can find the LuaJava class library and source code in: http://www.keplerproject.org/luajava/, use documentation, etc.

Download it and unzip it to include two files (I downloaded version 1.1): luajava-1.1.jar files and luajava-1.1.dll dynamic connection library files

It's easy to add luajava-1.1.jar to the ClassPath of your project so that the program can use the APIS it provides

LuaJava-1.1.dll is in trouble, you must add it to your Windows installation directory, such as you are using XP, installed on the C disk, then directly add it to C: \ WINDOWS directory. Of course, you can add it to the jre directory under your JDK.

Now, you can use the Lua script to implement dynamic expansion! But don't worry. Do you still have tools to write Lua scripts? Cannot be written using notepad?

You can use UltraEdit, but after you open the lua file with UE, you will find that it is similar to notepad, and there may be no new version that supports Lua scripts.) If your UE does not support it, then go to the official website of UltraEdit to download the Wordfiles file that supports Lua (http://www.ultraedit.com/files/wf/lua.txt files, is a personal file (lua.txt ). Open wordfile.txt in the ultraeditinstallation directory, paste the content in the lua.txt file to the end of wordfile.txt, save the disk, and OK, so there is more Lua IN THE UltraEdit syntax highlight item, you can choose to use it. Syntax highlighting in other languages is similar to this.

But if you are a Java developer, should you have used Eclipse? Can I directly write the Lua script in Eclipse? The answer is yes!

This is of course the powerful plug-in management function of Eclipse. You can download the luaeclipse plug-in to enable Eclipse to write Lua scripts, which can highlight your scripts, is it nice), you can download here: http://www.ideais.com.br/luaeclipse/

After the download and installation, you can create and compile EclipseLuaScript, pay attention to setting the LUA attributes in the preferences (I don't need to say it when installing the plug-in Eclipse ????)

Now everything is ready. Let's have a HelloWorld!
First, create a TestLua project in Eclipse, and then write the following program:

 
 
  1. Import org. keplerproject. luajava .*;
  2.  
  3. Public class Hello
  4. {
  5. Public static void main (String [] args)
  6. {
  7. LuaState L = LuaStateFactory. newLuaState ();
  8. L. openLibs ();
  9. System. out. println ("the Java program calls the Lua script ");
  10. // Load the script hello. lua and execute
  11. L. LdoFile ("res/hello. lua ");
  12. }
  13. }

Okay, the program is finished. Of course, it is saved as Hello. java. Note that this is Java code! This is the Java code that calls a script named hello. lua. below is the content of this script file (you can copy them directly to your hello. lua file ):

Basic Method

 
 
  1. Print ("You are currently using the LUA scripting language ")
  2. Print ("Let's take a look at it! \ N ")

Feature 1: assign values

 
 
  1. A = {1, 2}
  2. B =
  3. Print (a = B, ~ = B) -- Output true, false
  4. A = {1, 2}
  5. B = {1, 2}
  6. Print (a = B, ~ = B) -- outputs false, true

Feature 2, exchange

 
 
  1. A, B = 1, 2
  2. A, bb = B,
  3. Print ()
  4. Print (B)
  5.  
  6. Print ("Connect"... "string"... 2 ^ 3)
  7. Print (type (2 ))

While Loop

 
 
  1. I = 0
  2. Print ("while loop example ")
  3. While I <5
  4. Do
  5. Print (I );
  6. Ii = I + 1
  7. End

Repeat loop

 
 
  1. I = 0
  2. Print ("repeat loop example ")
  3. Repeat
  4. Print (I)
  5. Ii = I + 1
  6. Until I> = 5

For Loop

 
 
  1. Print ("for Loop example ")
  2. For I = 0, 5, 1
  3. Do
  4. Print (I)
  5. End
  6.  
  7. T1 = {}
  8. T1 [1] = 10
  9. Print (T1 [1])
  10.  
  11. Function fun (a, B ,...)
  12. Print ()
  13. Print (B)
  14. Print (arg [1])
  15. Print (arg [2])
  16. Print (arg [3])
  17. Return
  18. End
  19.  
  20. A, B = 2, 3
  21. Fun (a, B, 200,400,500)

Well, it doesn't matter if you don't understand the above script. Run it directly.

You can execute the compilation and execute the Hello. java program. The following output is displayed:

The Java program calls the Lua script. Now you are using the LUA script language. Let's take a look at it!

 
 
  1. True false
  2. False true
  3. 2
  4. 1
  5. Connection string 8
  6. Number
  7. While loop example
  8. 0
  9. 1
  10. 2
  11. 3
  12. 4
  13. Repeat loop example
  14. 0
  15. 1
  16. 2
  17. 3
  18. 4
  19. For Loop example
  20. 0
  21. 1
  22. 2
  23. 3
  24. 4
  25. 5
  26. 10
  27. 2
  28. 3
  29. 200
  30. 400
  31. 500

Iv. LUA. ORG official development tool

Lua 5.1.4is the latest version (complete and 2008.8.22), the source code is C, and lua_v5.1.4.23.exe is the installation environment of LUA5.1, including

Lua.exe this command can execute a compiled LUA Program

Luac.exe this command can compile a LUA source file into a bytecode file named LUAC. OUT.

SciTE \ SciTE.exe this is a LUA IDE

LExecutor. wlua this window program can execute a LUA source file

You can also run the script program on the page at this URL! Http://www.lua.org/cgi-bin/demo

V. LUA tutorial

Online Tutorial: http://manual.luaer.cn/

You can also download the lua-5.1 Chinese manual. chm to your local.

Summary: DetailsJ2luaThe content of the Script Programming learning tutorial is complete. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.