嘗試在URLClassLoader的仰視。你將需要繼承它。
下面是我的自定義類加載器的示例。
/** A <code>ClassLoader</code> that locates runtime & support libraries **/
final class MyClassLoader extends URLClassLoader {
MyClassLoader(String libStr, ClassLoader parent) throws MalformedURLException {
this(new File(libStr), parent);
}
MyClassLoader(File libDir, ClassLoader parent) throws MalformedURLException {
super(new URL[]{libDir.toURI().toURL()}, parent);
}
final private String[] accepted_lib_extensions = {".jar", ".zip", ".properties"};
void addDir(File dir) throws MalformedURLException {
FilenameFilter filenamefilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
String smallName = name.toLowerCase();
for (int i = 0; i < accepted_lib_extensions.length; i++) {
if (smallName.endsWith(accepted_lib_extensions[i])){
return true;
}
}
return false;
}
};
File[] jars = dir.listFiles(filenamefilter);
if (jars == null) return;
for (int i = jars.length-1; i >= 0; i--) {
if (jars[i].isFile()) {
final URL url = jars[i].toURI().toURL();
// This is key in adding your own classpath to JRE
addURL(url);
}
}
}
}
感謝您的回答。 wildcard.c中的實現很好並且易於遵循。 –
通配符擴展是Java 7中的一個標準嗎? –
不,通配符擴展甚至可能不會成爲標準。我懷疑它會在不久的將來,因爲它需要指定JVM進程從命令行接收的所有參數。 –