[armedbear-devel] java/abcl hello-world example

Alessio Stalla alessiostalla at gmail.com
Sun Aug 22 09:19:18 UTC 2010


On Sun, Aug 22, 2010 at 1:15 AM, David Dreisigmeyer
<david.dreisigmeyer at gmail.com> wrote:
> Hi Robert,
>
> I did see that (thanks for the link).  This example is included with
> the source code for abcl also (in examples/lisp-to-java).  I guess
> what I'd need help with is how to to make a jar file from the
> Main.java from examples/lisp-to-java (see below) file and then use
> that from abcl.
>
> Thanks again, I really appreciate it,
>
> -Dave
>
> import org.armedbear.lisp.*;
>
> public class Main
> {
>    /**
>     * This example creates an Interpreter instance, loads our
>     * lisp code from a file and then looks up a function defined
>     * in the loaded lisp file and executes the function.
>     *
>     * The function takes a single parameter and invokes a java method
>     * on the object provided. We provide our Main object as the parameter.
>     *
>     */
>    public static void main(String[] argv)
>    {
>        try
>            {
>                Main thisObject = new Main();
>                Interpreter interpreter = Interpreter.createInstance();
>                interpreter.eval("(load \"lispfunctions.lisp\")");
>                // the function is not in a separate package, thus the
>                // correct package is CL-USER. Symbol names are
>                // upper case. Package needs the prefix, because java
>                // also has a class named Package.
>                org.armedbear.lisp.Package defaultPackage =
>                    Packages.findPackage("CL-USER");
>                Symbol voidsym =
>                    defaultPackage.findAccessibleSymbol("VOID-FUNCTION");
>                Function voidFunction = (Function) voidsym.getSymbolFunction();
>                voidFunction.execute(new JavaObject(thisObject));
>            }
>        catch (Throwable t)
>            {
>                System.out.println("exception!");
>                t.printStackTrace();
>            }
>    }
>    public int addTwoNumbers(int a, int b)
>    {
>        return a + b;
>    }
> }

That's independent from ABCL; it's the same as for any Java library.
You need to compile the java file(s) with javac, with the libraries
you need (ABCL included) on the classpath, then if you want a jar, run
the jar program on the generated .class files; it has a command like
syntax inspired by tar. In your case, that means something like

javac -cp .:/path/to/abcl.jar your-java-files
jar cvf foo.jar .

assuming you're in a Unix-like environment and . is the source code
root of your project.

For more complex scenarios - projects consisting of many files and
depending on many libraries - you'll be better off using an IDE like
Eclipse or a build tool like Ant.

To use your jar with ABCL, include it in the classpath:

java -cp /path/to/abcl.jar:/path/to/your.jar org.armedbear.lisp.Main

hth,
Alessio Stalla




More information about the armedbear-devel mailing list