开发者

How can I embed Clojure in an RCP application

开发者 https://www.devze.com 2022-12-21 07:07 出处:网络
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

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 a开发者_如何学Pythonnyone know how to fix this?


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() ;


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号