我有一個將在運行時編譯的java源代碼。 我編譯源代碼(目前作爲一個字符串),並將其加載到如下的一類:調用在運行時編譯的類的自定義方法
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
// Load and instantiate compiled class.
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("TakeAdvantageOftMapComponents", true, classLoader);
這個類包含了我需要調用一個名爲runJob
的方法。通常我會這樣做:
TakeAdvantageOftMapComponents c = new TakeAdvantageOftMapComponents();
c.runJob(new string[]);
現在這個相同的方法存在於我在運行時編譯的類中。但是,我怎麼稱呼它?
反思,也許? – awksp
我想出了使用Relection - 基於這裏給出的答案:我想出了使用Relection - 基於給出的答案[這裏] [1] http://stackoverflow.com/questions/160970/how-do -i-invoke -a-java-method-when-method-the-method-name-as-a-string – Aneesh