LuaJ导入Lua方法 [英] LuaJ Import Lua Methods

查看:353
本文介绍了LuaJ导入Lua方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LuaJ,并且我有一个 .lua 文件,其中包含许多函数。如何导入这些函数以在LuaJ中使用Java?

I'm using LuaJ, and I have a .lua file filled with a bunch of functions. How do I import these functions to use in Java with LuaJ?

推荐答案

一种选择是将文件编译成Java代码和导入那个。另一种方法是使用嵌入式解释器直接从Java代码调用Lua文件。

One option would be to compile the file into Java code and import that. Another would be to simply invoke the Lua file directly from your Java code using the embeddable interpreter.

*编辑*

下载的文档中有很好的例子。要从Java中运行脚本,你可以这样做:

There are good examples in the downloaded documentation. To run a script from within Java you would do something like this:

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class LuaTest {
    public static void main(String[] args) throws Exception {
        String scriptFilePath = "/Users/developer/work/luaj-2.0.2/examples/lua/hello.lua";

        Reader reader = new FileReader(new File(scriptFilePath));
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine e = mgr.getEngineByExtension(".lua");
        Object result = e.eval(reader);
    }
}

要将Lua脚本编译成Java源代码,会做这样的事情:

To compile a Lua script into Java source code, you would do something like this:

java -cp lib/luaj-jse-2.0.2.jar lua2java -s examples/lua -d . hello.lua
javac -cp lib/luaj-jse-2.0.2.jar hello.java

这些示例几乎取自您下载Luaj时获得的README.html。我强烈建议端到端地阅读它,以便更好地掌握可用的功能。

These examples are pretty much taken from the README.html which you get when you download Luaj. I would highly recommend reading it end to end to get a good grasp of the available functionality.

这篇关于LuaJ导入Lua方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆