How can I embed Clojure in an RCP application

情到浓时终转凉″ 提交于 2019-12-04 11:46:26

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!