我知道我必須做一些事情,只是腦死人愚蠢的這不起作用,但我在一個情況下,我想動態地加載到正在運行的服務器的行爲。我選擇groovy作爲我的工具來做到這一點。該行爲將需要引用服務器類路徑上的類,例如我的模型對象以及像Freemarker這樣的第三方庫。Groovy,嵌入到Java中,回撥到Java
我把這個愚蠢的POC扔在一起來展示可行性。儘管事實上我將GroovyClassPath的父類路徑設置爲當前,但我無法獲得groovy腳本來解決Java類「ThingyDoodle」和「Fooable」。
public class GroovyTest
{
public static void main (String [ ] argv) throws Throwable
{
// Setting parent classloader!
// also tried plain old "GroovyTest.class.getClassLoader()" as well
GroovyClassLoader gcl = new GroovyClassLoader (Thread.currentThread().getContextClassLoader()) ;
String src =
"class Arf implements Fooable {
public String foo () {
return new ThingyDoodle().doStuff('Arf');}}" ;
Class clazz = gcl.parseClass(src, "AppleSauce.groovy");
Object aScript = clazz.newInstance();
Fooable myObject = (Fooable) aScript;
System.out.println (myObject.foo()) ;
}
public static interface Fooable { public String foo () ; }
public static class ThingyDoodle
{
public String doStuff (String input)
{
return "Hi Worlds" ;
}
}
}
我在這裏做錯了什麼?
Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
AppleSauce.groovy: 1: unable to resolve class Fooable
@ line 1, column 1.
class Arf implements Fooable { public String foo () { return new ThingyDoodle().doStuff('Arf');}}
^
AppleSauce.groovy: 1: unable to resolve class ThingyDoodle
@ line 1, column 63.
ublic String foo () { return new Thingy
^
好的,這是令人尷尬的:) – billmill 2012-04-26 12:30:19
非常感謝!這是我需要的。 – billmill 2012-04-26 12:30:37
不要出汗。我擅長在別人身上發現錯誤[bugs],但是對我自己的[bugs]一目瞭然...... :) – Nicholas 2012-04-26 14:47:01