views:

384

answers:

3

I am currently using javascript to add scripting to an Eclipse RCP application, but I would prefer to be able to use Clojure. However, I have run into classpath difficulties because while Eclipse can find the Clojure classes, Clojure itself can't.

The plugin activator's start method:

public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this ;
    Class.forName("clojure.lang.RT") ;
    JSController.startup() ;
}

doesn't raise a class not found exception for clojure.lang.RT, but for clojure/core__init which is in the same place.

java.io.FileNotFoundException: Could not locate clojure/core__init.class or clojure/core.clj on classpath:
        at clojure.lang.RT.load(RT.java:402)
        at clojure.lang.RT.load(RT.java:371)
        at clojure.lang.RT.doInit(RT.java:406)
        at clojure.lang.RT.<clinit>(RT.java:292)

The RCP application is based on Eclipse version 3.1

Does anyone know how to fix this?

+1  A: 

You need to wrap the Clojure JAR in an OSGi bundle to use it in an Eclipse RCP application. Fortunately, this has already been done by the Counterclockwise Eclipse plugin.

Markus Knittig
Kind of, but I wanted a plugin that would start a swank server on startup; I would just tweak the dependencies (to avoid problems accessing the application's classes), and drop it in.Counterclockwise didn't quite do that.
TomSW
+1  A: 

Perhaps you need to add necessary paths to your Bundle-ClassPath in plugin's MANIFEST.MF so Clojure's jars can be found by class loader. Easiest way to do that in eclipse is to open plugin.xml in editor and go to "Runtime" tab.

AFAIK you do not need to force class loading with Class.forName("clojure.lang.RT"); - it looks extraneous to me here.

I made my plugin work with Clojure by adding dependencies to Counterclockwise plugins (ccw.clojure and ccw.clojurecontrib).

Petr Gladkikh
The Class.forName is what the ccw.clojure plugin does. And the error was coming from there so extraneous or not it's at least interesting :)
TomSW
Ok. Now I think that this may be required to force Clojure compilation via some class loader - just guessing.
Petr Gladkikh
+1  A: 

It was much simpler than I thought: I had assumed that when activating the bundle / plugin, the thread's classloader would be the one that loaded the plugin. It's not, it's the application classloader.

So the solution is simple:

Runnable cljRunner = new Runnable(){
    public void run(){
        Thread thisThread = Thread.currentThread() ;
        ClassLoader savedCL = thisThread.getContextClassLoader() ;              
        thisThread.setContextClassLoader(Activator.class.getClassLoader()) ;
        try {
            clojure.lang.Compiler.load(
                new java.io.StringReader(
                        "(require 'clojure.main)\n" +
                        "(require 'swank.swank)\n"  +
                        "(clojure.main/with-bindings\n" +
                        "    (swank.swank/start-server \"nul\" :encoding \"utf-8\" :port 9999))"
                )) ;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        thisThread.setContextClassLoader(savedCL) ;
    }
} ;
cljThread = new Thread(cljRunner) ;
cljThread.start() ;
TomSW