[plt-scheme] Re: Interfacing with Java code

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sun Sep 17 14:14:44 EDT 2006

Kathy Gray wrote:
>> Message: 8
>> Date: Thu, 6 Jul 2006 08:40:16 +0100
>> From: "Paulo J. Matos" <pocmatos at gmail.com>
>> Subject: [plt-scheme] Interfacing with Java code
>> To: "PLT Scheme Mailing List" <plt-scheme at list.cs.brown.edu>
>> Message-ID:
>>     <11b141710607060040j3c070e78wa4c927badb30f942 at mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>> Hi,
>> Is there any way in current DrScheme to interface with Java code?
>> Something like ffi for Java? I know I can use Kawa or other Java-based
>> Scheme but I would prefer PLT-Scheme if that were to be possible.
> Depending on your needs, there is the ProfessorJ compiler with
> DrScheme, which contains a language extension to facilitate
> interoperation between Java and Scheme.
> The compiler doesn't support static nested classes, switch, threads,
> or C native methods.
> If you try this route, I'd be happy to offer assistance in hooking
> things up.
After a debate with a friend and due to some free time I made a set of
"ffi" bindings to java. Basically it starts up a JVM inside the mzscheme
process and then uses the normal ffi to interact with it from mzscheme.
I hope replying to this ancient email is ok.


Here is a small demo of what it allows

(require (lib "foreign.ss"))
(require "src/scheme/javabridge.ss")

(when (not (start-java-vm '()))
  (printf "Major error\n")

(define make-string-buffer (make-java-constructor java.lang.StringBuffer))

(let ((n (make-string-buffer)))
  (if n
      (printf "StringBuffer = '~a'\n" (to-string n))
      (let ((change (make-java-method "append" (_jfun _jstring ->
      (change n "Hello world")
      (printf "Changed string = '~a'\n" (to-string n))))
    (printf "Could not make string\n")))

It handles arbitrary lists of arguments through var args( va_list ). Ill
make this a planet package if anyone thinks its useful and hasn't been
done before.

Posted on the users mailing list.