How can I embed Clojure in an RCP application

烈酒焚心 提交于 2019-12-06 04:11:57

问题


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:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/2352242/how-can-i-embed-clojure-in-an-rcp-application

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