Methods Summary |
---|
public static java.lang.ClassLoader | getClassLoader(java.io.File[] dirs, java.io.File[] jarDirs, java.lang.ClassLoader parent)Returns a new class loader based on the given directory paths and
the jar files & zip files found under jar directory.
URLClassLoader loader = null;
URL[] urls = getUrls(dirs, jarDirs);
if (urls != null) {
if (parent != null) {
loader = new URLClassLoader(urls, parent);
} else {
loader = new URLClassLoader(urls);
}
}
return loader;
|
public static java.util.List | getUrlList(java.io.File[] dirs, java.io.File[] jarDirs)Returns a list of urls that contains ..
i. all the valid directories from the given directory (dirs) array
ii. all jar files from the given directory (jarDirs) array
iii. all zip files from the given directory (jarDirs) array
This is similar to getUrls(File[], File[])
return getUrlList(dirs, jarDirs, false);
|
public static java.util.List | getUrlList(java.io.File[] dirs, java.io.File[] jarDirs, boolean ignoreZip)Returns a list of urls that contains ..
i. all the valid directories from the given directory (dirs)
array
ii. all jar files from the given directory (jarDirs) array
iii. all zip files from the given directory (jarDirs) array if
not ignoring zip file (ignoreZip is false).
List list = new ArrayList();
// adds all directories
if (dirs != null) {
for (int i=0; i<dirs.length; i++) {
File dir = dirs[i];
if (dir.isDirectory() || dir.canRead()) {
list.add( dir.getCanonicalPath() );
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE,
"Adding directory to class path:"
+ dir.getCanonicalPath());
}
}
}
}
// adds all the jars
if (jarDirs != null) {
for (int i=0; i<jarDirs.length; i++) {
File jarDir = jarDirs[i];
if (jarDir.isDirectory() || jarDir.canRead()) {
File[] files = jarDir.listFiles();
for (int j=0; j<files.length; j++) {
File jar = files[j];
if ( FileUtils.isJar(jar) ||
(!ignoreZip && FileUtils.isZip(jar)) ) {
list.add( jar.getCanonicalPath() );
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE,
"Adding jar to class path:"
+ jar.getCanonicalPath());
}
}
}
}
}
}
return list;
|
public static java.net.URL[] | getUrls(java.io.File[] dirs, java.io.File[] jarDirs)Returns an array of urls that contains ..
i. all the valid directories from the given directory (dirs) array
ii. all jar files from the given directory (jarDirs) array
iii. all zip files from the given directory (jarDirs) array
URL[] urls = null;
List list = new ArrayList();
// adds all directories
if (dirs != null) {
for (int i=0; i<dirs.length; i++) {
File dir = dirs[i];
if (dir.isDirectory() || dir.canRead()) {
URL url = dir.toURI().toURL();
list.add(url);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE,
"Adding directory to class path:" + url.toString());
}
}
}
}
// adds all the jars
if (jarDirs != null) {
for (int i=0; i<jarDirs.length; i++) {
File jarDir = jarDirs[i];
if (jarDir.isDirectory() || jarDir.canRead()) {
File[] files = jarDir.listFiles();
for (int j=0; j<files.length; j++) {
File jar = files[j];
if ( FileUtils.isJar(jar) || FileUtils.isZip(jar) ) {
list.add(jar.toURI().toURL());
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE,
"Adding jar to class path:" + jar.toURL());
}
}
}
}
}
}
// converts the list to an array
if (list.size() > 0) {
urls = new URL[list.size()];
urls = (URL[]) list.toArray(urls);
}
return urls;
|
public static java.net.URL[] | getUrlsFromClasspath(java.lang.String classpath)get URL[] from classpath
catches exception for wrong files
final List<URL> urls = new ArrayList<URL>();
if (classpath == null) {
return EMPTY_URL_ARRAY;
}
// tokenize classpath
final StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
while (st.hasMoreTokens()) {
try {
File f = new File(st.nextToken());
urls.add(f.toURI().toURL());
} catch(Exception e) {
_logger.log(Level.WARNING,
"loader.unexpected_error_while_creating_urls",e);
}
}
// converts the list to an array
URL[] ret;
if (urls.size() > 0) {
ret = urls.toArray(new URL[]{});
} else {
ret = new URL[0];
}
return ret;
|
public static void | main(java.lang.String[] args)Unit test code.
try {
URL[] urls = getUrls(new File[] {new File(args[0])},
new File[] {new File(args[1])});
for (int i=0; i<urls.length; i++) {
System.out.println(urls[i]);
}
URLClassLoader loader = (URLClassLoader)
getClassLoader(new File[] {new File(args[0])},
new File[] {new File(args[1])}, null);
//Class c = Class.forName(args[2], true, loader);
Class c = loader.loadClass(args[2]);
System.out.println("Loaded: " + c.getName());
System.out.println("Loaded class has the following methods...");
java.lang.reflect.Method[] m = c.getDeclaredMethods();
for (int i=0; i<m.length; i++) {
System.out.println(m[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
|