Using Scheme from Java

This tutorial assumes you have some familiarity with Scheme and Java. It shows how to access Java objects from Scheme.

SILK adds several functions to Scheme that provide access to Java. In each case, the arguments can be either strings or symbols.

(class name)
Returns the Java Class named name. Example:
> (class "java.lang.Class")
class java.lang.Class
> (class "javax.swing.JFrame")
class javax.swing.JFrame
(constructor class parameterType ...)
Returns the constructor for class class that takes parameters whos types are named parameterType .... For example, here we construct a hash table of size 20:
> (define ht (constructor "java.util.Hashtable" "int"))
ht
> (define h (ht 20))
h
> h
{}            ; An empty HashTable.
(method name class parameterType ...)
Returns a procedure which when called will invoke the method named name on the class named class that takes parameters whos types are named parameterType .... For example, here we make a procedure (put) than can add objects to a hash table:
> (define put (method "put" "java.util.Hashtable" "java.lang.Object" "java.lang.Object"))
put
> (put h "fern" 3)
()
> (put h "bar" 4)
()
> h
{bar=4, fern=3}

(import class)
This is roughly like the import ... ; statement in Java. Lets consider importing the Hashtable class, for example:
> (import "java.util.Hashtable")
importing java.util.Hashtable in 40 ms.
#t
This has the following effect:

GC monitor example

For an example see ../src/elf/GCMonitor.scm which constructs a garbage collection monitor. It uses ../src/elf/Listen.java to provide Java event listeners that execute Scheme code.