# Compiling Lucene with GCJ

# Background

[Lucene](http://lucene.apache.org/) is a open-source search library written in Java.

[<span class="caps">GCJ</span>](http://gcc.gnu.org/java/) is a Java to native-executable compilter. As shown in [a LinuxJournal article](http://www.linuxjournal.com/article/4860), using gcj is similar to using gcc.

# Versions Used

As of 1/26/08, the latest version of Lucene is 2.3.0. It needs Java SE 1.4 to compile and run. <span class="caps">GCJ</span> 3.3.6 doesn’t work because apparently, it doesn’t support classes in Java 1.4 (e.g. the new regular expressions classes and functions and <span class="caps">NIO</span> stuff). <span class="caps">GCJ</span> 3.4.5 and 3.4.6 works. I haven’t tried version 4.x yet.

## Installing Lucene

Get [Lucene’s binary](http://www.apache.org/dyn/closer.cgi/lucene/java/). You need lucene-core-2.3.0.jar. There is another <span class="caps">JAR</span> file lucene-demos-2.3.0.jar that contains the demos, which are useful to check that your copy of <span class="caps">GCJ</span> is working.

## Installing <span class="caps">GCC</span>

You get <span class="caps">GCJ</span> by installing <span class="caps">GCC</span>. [A how-to of installing <span class="caps">GCC</span> 3.3.6](http://www.linuxfromscratch.org/blfs/view/cvs/general/gcc3.html) can guide you through that. Basically there are four steps in the process (after downloading and uncompressing the source files):

- configure (I used ./configure —prefix= —with-as=/usr/ccs/bin/ —with-ld= —disable-nls —enable-libgcj
- make bootstrap
- make check (I think this one is optional)
- make install

If you use Windows, you don’t need the steps above. Just get MinGW and install <span class="caps">GCC</span> from it.

## Testing

Lucene comes with a pair of indexer and searcher in their demo collection. They have [a page that explains how to compile and run the demo](http://lucene.apache.org/java/docs/demo.html).

You can compile the indexing with the following command:

```
/path_of_gcj/bin/gcj lucene-core-2.3.0.jar lucene-demos-2.3.0.jar -o indexer --main=org.apache.lucene.demo.IndexFiles
```

That should create an executable file called indexer in the same directory. Run it and it should show the usage and exit.

When you run the indexer, it might complain that “ld.so.1: indexfiles: fatal: libgcj.so.5: open failed: No such file or directory”. Basically, it tries to find this run-time shared library but can’t find it. You’ll have to tell it where to look at. E.g. in bash, use the following command:

```
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path_of_gcj/lib
```

You can compile and run the searching in a similar way.