博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ClassLoader 学习 - MyURLClassLoader
阅读量:4146 次
发布时间:2019-05-25

本文共 1251 字,大约阅读时间需要 4 分钟。

用户可以通过 URLClassLoader 动态载入一个Class文件或者 jar 包
package
 com.humpic.framework.classloader;
import
 java.io.File;
import
 java.io.IOException;
import
 java.net.
*
;
/**
 * 自定义 ClassLoader
 * 
 * 
@since
 2007-11-13
 * 
@author
 Chen Guoqiang
 
*/
public
 
class
 MyURLClassLoader 
extends
 URLClassLoader {
    
public
 MyURLClassLoader() {
        
super
(
new
 URL[
0
], ClassLoader.getSystemClassLoader());
    }
    
public
 
void
 addFiles(String[] files) {
        
if
 (files 
==
 
null
 
||
 files.length 
<=
 
0
) {
            
return
;
        }
        
for
 (
int
 i 
=
 
0
; i 
<
 files.length; i
++
) {
            
if
 (files[i] 
!=
 
null
 
&&
 files[i].length() 
>
 
0
) {
                addFile(files[i]);
            }
        }
    }
    
public
 
void
 addFiles(File[] files) {
        
if
 (files 
==
 
null
 
||
 files.length 
<=
 
0
) {
            
return
;
        }
        
for
 (
int
 i 
=
 
0
; i 
<
 files.length; i
++
) {
            addFile(files[i]);
        }
    }
    
public
 
void
 addURLs(URL[] urls) {
        
if
 (urls 
==
 
null
 
||
 urls.length 
<=
 
0
) {
            
return
;
        }
        
for
 (
int
 i 
=
 
0
; i 
<
 urls.length; i
++
) {
            addURL(urls[i]);
        }
    }
    
public
 
void
 addFile(String file) {
        
try
 {
            addFile(
new
 File(file).getCanonicalFile());
        } 
catch
 (IOException e) {
            
throw
 
new
 RuntimeException(e);
        }
    }
    
public
 
void
 addFile(File file) {
        
try
 {
            addURL(file.toURL());
        } 
catch
 (MalformedURLException e) {
            
throw
 
new
 RuntimeException(e);
        }
    }
    
public
 
void
 addURL(URL url) {
        System.out.println(url);
        
super
.addURL(url);
    }
}

转载地址:http://vznti.baihongyu.com/

你可能感兴趣的文章
第六章 背包问题——01背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>
高性能服务器设计
查看>>
性能扩展问题要趁早
查看>>
MySQL-数据库、数据表结构操作(SQL)
查看>>
OpenLDAP for Windows 安装手册(2.4.26版)
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
JSP的内置对象及方法
查看>>
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC和Struts2的比较
查看>>
Hibernate和IBatis对比
查看>>