<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Incrementalist &#187; lua</title>
	<atom:link href="http://retrovirus.com/incr/category/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://retrovirus.com/incr</link>
	<description></description>
	<lastBuildDate>Tue, 03 Jul 2007 04:46:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Building LuaJava on Mac OS X</title>
		<link>http://retrovirus.com/incr/2005/08/building-luajava/</link>
		<comments>http://retrovirus.com/incr/2005/08/building-luajava/#comments</comments>
		<pubDate>Sun, 21 Aug 2005 18:48:29 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[mac]]></category>

		<guid isPermaLink="false">/?p=24</guid>
		<description><![CDATA[In my previous post about coroutines, I mentioned that I&#8217;d be looking into LuaJava (a bridge between Java and the Lua language) as a way to get coroutine behavior in a Java environment.  Since I had some time this afternoon, I decided to get LuaJava up and running on my PowerBook.  Here are [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous <a href="http://retrovirus.com/incr/2005/08/coroutines/">post about coroutines</a>, I mentioned that I&#8217;d be looking into <a href="http://www.keplerproject.org/luajava/">LuaJava</a> (a bridge between Java and the <a href="http://www.lua.org/">Lua</a> language) as a way to get coroutine behavior in a Java environment.  Since I had some time this afternoon, I decided to get LuaJava up and running on my PowerBook.  Here are some steps that you can follow to build LuaJava on Mac OS X 10.4.2 Tiger.  (I&#8217;ve also successfully tested these instructions on Mac OS X  10.3.9 Panther.)</p>

<p>LuaJava requires Lua 5.0, so first we need to download the <a href="http://www.lua.org/ftp/lua-5.0.tar.gz">Lua 5.0 source</a> and build it:</p>

<pre><code>% tar xzvf lua-5.0.tar.gz
% cd lua-5.0
% make
% sudo make install
</code></pre>

<p>Next, we need to build LuaJava.  Download <a href="http://luaforge.net/frs/download.php/947/luajava-1.0.tar.gz">LuaJava 1.0</a>, then extract it and switch to its directory:</p>

<pre><code>% tar xzvf luajava-1.0.tar.gz
% cd luajava-1.0
</code></pre>

<p>Since LuaJava&#8217;s <code>config</code> file comes set up for Linux by default, we need to edit it to Mac OS X-friendly settings.  Comment the following lines:</p>

<pre><code>#JDK= $(JAVA_HOME)
#LIB_EXT= .so
#LIB_OPTION= -shared
#DLLIB= -ldl
</code></pre>

<p>and uncomment the corresponding ones:</p>

<pre><code>JDK=/Library/Java/Home
LIB_EXT= .jnilib
LIB_OPTION= -dynamiclib -all_load
</code></pre>

<p>We also need to change the <code>LIB_LUA</code> line to read:</p>

<pre><code>LIB_LUA=/usr/local/lib/liblua.a /usr/local/lib/liblualib.a
</code></pre>

<p>With those changes in place, we can just type</p>

<pre><code>% make
</code></pre>

<p>and apart from a few JavaDoc warnings, everything should go smoothly.  To test it, we can fire up the LuaJava Console:</p>

<pre><code>% java -cp "luajava-1.0.jar" org.keplerproject.luajava.Console
API Lua Java - console mode.
&gt; print('Hello, world!')
Hello, world!
&gt; exit
</code></pre>

<p>OK, that looks good.  How about the included tests?</p>

<pre><code>% cd test
% ./runawttest.sh
% ./runswingtest.sh
</code></pre>

<p>Again, working fine.  There are a couple of other Lua test files in the <code>test</code> directory that we can run like so:</p>

<pre><code>% java -cp "../luajava-1.0.jar" -Djava.library.path=.. 
    org.keplerproject.luajava.Console testMemory.lua
</code></pre>

<p>(replace <code>testMemory.lua</code> with the name of the file you want to run)</p>

<p>OK, let&#8217;s try creating a program of our own.  There&#8217;s a decent Hello World on the <a href="http://www.keplerproject.org/luajava/examples.html">LuaJava examples page</a>, so (after switching back to the <code>luajava-1.0</code> directory), create the <code>Hello.java</code> and <code>hello.lua</code> files depicted on the examples page.  You&#8217;ll need to add an import line to the top of <code>Hello.java</code> so that Java knows where to find all the LuaJava objects:</p>

<pre><code>import org.keplerproject.luajava.*;
</code></pre>

<p>Once you have the files, compile the Java class:</p>

<pre><code>% javac -classpath luajava-1.0.jar Hello.java
</code></pre>

<p>Now run it:</p>

<pre><code>% java -cp luajava-1.0.jar:. Hello
</code></pre>

<p>If everything is working correctly, you should see:</p>

<pre><code>Hello World from Lua!
Hello World from Java!
</code></pre>

<p>Huzzah!  Now we have all the pieces we need to start embedding Lua functionality in Java code.</p>
]]></content:encoded>
			<wfw:commentRss>http://retrovirus.com/incr/2005/08/building-luajava/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
