我正在嘗試將類動態加載到組件中。我正在使用文件選擇器來選擇將要加載的.JAR文件,然後使用選項窗格來獲取類的名稱。Java動態加載類
我已經蒐羅互聯網尋找如何將Java文件轉換爲一個URL,以便將其加載到URLClassLoader的,我想出了:
File myFile = filechooser.getSelectedFile();
String className = JOptionPane.showInputDialog(
this, "Class Name:", "Class Name", JOptionPane.QUESTION_MESSAGE);
URL myUrl= null;
try {
myUrl = myFile.toURL();
} catch (MalformedURLException e) {
}
URLClassLoader loader = new URLClassLoader(myUrl);
loader.loadClass(className);
現在我得到一個「無法找到符號'將URL加載到URLClassLoader中的錯誤
是'拖了'一個字?谷歌唯一建議的是'trolled':-) http://www.urbandictionary.com/define.php?term=trolled – 2010-08-27 04:48:14
@seanizer:「trawled」 - 作爲搜索的一部分。 – trashgod 2010-08-27 12:51:19
@trashgod聽起來好多了... – 2010-08-27 13:10:14