All,

I've had success using JNA to call system libraries I thought I'd post
an example snippet in case it helps someone else. The below demo
requires a clojure-contrib.jar from http://sf.net/projects/clojure-contrib
and a jna.jar from
https://jna.dev.java.net/ (be sure it's actually, jna.jar not one of
the platform specific jar -- I got stuck for a while trying to get the
platform specific jar working when the generic jna.jar works fine).
The below is *NIX specific and runs fine on my Gentoo Linux x86_64
system with Java 1.6.0.

JNA is slower than a hard coded JNI interface but for the 99% of
system poking that one might want to do (iNotify, ioctls, UDS, etc.)
it should work very nicely. Feel free to ask questions in case the
example isn't clear but be forewarned -- the below encapsulates 100%
of my JNA + Clojure knowledge so far. ;-)

; glibc from Clojure via JNA
(ns jnatest
  (:import (com.sun.jna Library Native))
  (:use (clojure.contrib gen-interface)))

(gen-and-load-interface 'jnatest.CLibrary [Library]
                        ['getppid [] Integer]
                        ['getpid [] Integer]
                        ['sleep [Integer] Void])

(def glibc (Native/loadLibrary "c" jnatest.CLibrary))

(println "Parent's PID: " (.getppid glibc))
(println "My PID: " (.getpid glibc))

(println "Now show that we can sleep:")
(time (.sleep glibc 2))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to