问题描述
我目前使用javascript来添加脚本到Eclipse RCP应用程序,但我更喜欢能够使用Clojure。但是,我遇到了类路径的困难,因为虽然Eclipse可以找到Clojure类,但Clojure本身不能。
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() ;
}
不会引发类未找到异常 clojure.lang.RT ,但针对位于同一位置的 clojure / core__init 。
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)
RCP应用程序基于Eclipse版本3.1
The RCP application is based on Eclipse version 3.1
有人知道如何解决这个问题吗?
Does anyone know how to fix this?
推荐答案
我想:我假设当激活bundle / plugin,线程的类加载器将是加载插件的那个。
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() ;
这篇关于如何在RCP应用程序中嵌入Clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!