[armedbear-cvs] r12908 - trunk/abcl/examples/pure-lisp-to-java

Ville Voutilainen vvoutilainen at common-lisp.net
Mon Aug 30 17:57:44 UTC 2010


Author: vvoutilainen
Date: Mon Aug 30 13:57:43 2010
New Revision: 12908

Log:
Add an example for using a java class with CLASSPATH, invoking java
functions from lisp code and finding the java class automagically
as it's in CLASSPATH.


Added:
   trunk/abcl/examples/pure-lisp-to-java/
   trunk/abcl/examples/pure-lisp-to-java/Main.java   (contents, props changed)
   trunk/abcl/examples/pure-lisp-to-java/README
   trunk/abcl/examples/pure-lisp-to-java/lispfunctions.lisp   (contents, props changed)

Added: trunk/abcl/examples/pure-lisp-to-java/Main.java
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/pure-lisp-to-java/Main.java	Mon Aug 30 13:57:43 2010
@@ -0,0 +1,39 @@
+/*
+ * Main.java
+ *
+ * Copyright (C) 2008 Ville Voutilainen
+ * $Id$
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+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 int addTwoNumbers(int a, int b)
+    {
+	return a + b;
+    }
+}

Added: trunk/abcl/examples/pure-lisp-to-java/README
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/pure-lisp-to-java/README	Mon Aug 30 13:57:43 2010
@@ -0,0 +1,38 @@
+ABCL Examples Building and Running Instructions
+===============================================
+
+To compile 
+
+    cmd$ javac  -cp ../../dist/abcl.jar  Main.java
+
+where the "../../../dist/abcl.jar" represents the path to your
+abcl.jar file, which is built via the Ant based build.  This path
+could be slightly different depending on how the system was
+constructed, and possibly due to operating system conventions for
+specifying relative paths.  However you resolve this locally, we'll
+refer to this as '$ABCL_ROOT/dist/abcl.jar' for the rest of these
+instructions.
+
+This compiles the Java source file "Main.java" into a JVM runtime or
+class file named "Main.class".
+
+To run the example (Main.class for example) from a Unix-like OS use:
+
+    cmd$ export CLASSPATH=.
+    cmd$ $ABCL_ROOT/abcl
+
+then, in abcl repl, use:
+(load "lispfunctions")
+(void-function)
+
+or in Windows use:
+
+    cmd$  set CLASSPATH=.
+    cmd$  $ABCL_ROOT/abcl
+
+then, in abcl repl, use:
+(load "lispfunctions")
+(void-function)
+
+This will result in the Main class being found from the CLASSPATH, and you
+can invoke the functions of the Main class from lisp code.

Added: trunk/abcl/examples/pure-lisp-to-java/lispfunctions.lisp
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/pure-lisp-to-java/lispfunctions.lisp	Mon Aug 30 13:57:43 2010
@@ -0,0 +1,34 @@
+;;; lispfunctions.lisp
+;;;
+;;; Copyright (C) 2008 Ville Voutilainen
+;;; $Id$
+;;;
+;;; This program is free software; you can redistribute it and/or
+;;; modify it under the terms of the GNU General Public License
+;;; as published by the Free Software Foundation; either version 2
+;;; of the License, or (at your option) any later version.
+;;;
+;;; This program is distributed in the hope that it will be useful,
+;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with this program; if not, write to the Free Software
+;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+; we need to get the
+; 1) class (Main)
+; 2) classes of the parameters (int)
+; 3) method reference (getting that requires the class
+; of our object and the classes of the parameters
+
+; After that we can invoke the function with jcall,
+; giving the method reference, the object and the parameters.
+; The result is a lisp object (no need to do jobject-lisp-value), 
+; unless we invoke the method
+; with jcall-raw. 
+(defun void-function ()
+  (let* ((result (jstatic "addTwoNumbers" "Main" 2 4)))
+    (format t "in void-function, result of calling addTwoNumbers(2, 4): ~a~%" result)))
+




More information about the armedbear-cvs mailing list