realsmy
久城的学习室
导航
BlogJava
首页
新随笔
联系
聚合
管理
公告
学习是一个过程
软件是一种态度
欢迎大家批评指正
常用链接
我的随笔
我的文章
我的评论
我的参与
最新评论
留言簿
(18)
给我留言
查看公开留言
查看私人留言
随笔分类
EXT&Extjs(3)
(rss)
Flex&ActionScript(2)
(rss)
GOF设计模式(3)
(rss)
JavaTest(19)
(rss)
Java理解笔记(9)
(rss)
Java转载(5)
(rss)
Jsp学习(3)
(rss)
XML(3)
(rss)
其他技术学习(5)
(rss)
小知识点(3)
(rss)
数据库学习(8)
(rss)
日语学习(4)
(rss)
程序人生(22)
(rss)
翻译(1)
(rss)
软件工程(5)
(rss)
问题(2)
(rss)
随笔档案
2008年7月 (1)
2008年3月 (3)
2008年2月 (3)
2008年1月 (5)
2007年12月 (7)
2007年11月 (2)
2007年10月 (5)
2007年9月 (11)
2007年8月 (2)
2007年7月 (1)
2007年6月 (2)
2007年5月 (12)
2007年4月 (16)
2007年3月 (2)
2006年12月 (1)
2006年11月 (5)
2006年10月 (9)
2006年9月 (11)
新闻档案
2007年9月 (1)
相册
Class Diagram
Friends
GIF
Neusoft
曾经深爱
收藏夹
我的收藏夹(6)
(rss)
BLOG
【爪哇咖啡馆】
ajaxbbs
BeanSoft
DUDU
java朋友
knowNothing
Rosicky
Spring Rain
小可爱
徒步穿越世界
我为J狂
良葛格
花半里
黑蝙蝠
JAVA学习网站
CSDN社区
IT PUB
JAVA RESEARCH
Java World@tw
JAVA2S
JAVAEYE
java学习室
java开源大全
J道论坛
matrix-与JAVA共舞
中国Eclipse社区
吉林IT信息网
太平洋网络学院
学习网站
helloajax
JStang
w3schools
中国RIA开发者论坛
网页特效
我的流水帐
2005.11-2005.12
2005.11-2006.8
2006.12
2006.12-2007.6
2006.7-2006.8
2006.8
2007.6
我的老师
Teacher Su
Teacher Yao
战友^-^
彭俊
糖糖的书房
静儿
社区
积分与排名
积分 - 78808
排名 - 95
最新评论
1. re: Extjs学习笔记 --- 实战
学习
--dafei
2. re: PL/SQL 培训项目实践与练习(一) 整理笔记
你的文章写的很好
对于plsql的学习也很有帮助
谢谢 也希望你继续努力写出更好的文章
--ljy
3. re: Extjs实战 --- 发布小纸条 (附下载)
heihei
--ext扩展类写的不错,我收下了呵呵!改成php的
4. re: 一道JS小考题
评论内容较长,点击标题查看
--ayiaman
5. re: 坦克游戏——实现坦克的移动和子弹的发射
评论内容较长,点击标题查看
--aaaa
阅读排行榜
1. Extjs学习笔记 --- 实战(3935)
2. Extjs学习笔记 --- 初篇(3690)
3. Extjs实战 --- 发布小纸条 (附下载)(2301)
4. JAVA加密解密---自定义类加载器应用(2146)
5. 读J道一经典帖有感(2001)
评论排行榜
1. 谈中国软件外包(21)
2. Extjs实战 --- 发布小纸条 (附下载)(13)
3. 参与第一个外包项目总结(10)
4. GoF设计模式学习笔记(一)---工厂模式(9)
5. 读J道一经典帖有感(8)
JAVA CLASS LOADING技术研究---整理后的代码
以下是整理后的代码部分,欢迎批评指正。
MyClassLoader.java
/**/
/*
* @MyClassLoader.java 07/04/17
*
* Copyright Zhao Jiucheng. All rights reserved.
*/
package
com.neusoft.classloader;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.util.Hashtable;
import
java.util.jar.JarEntry;
import
java.util.jar.JarInputStream;
/** */
/**
* A class loader is an object that is responsible for loading classes. Given
* the binary name of a class, a class loader should attempt to locate or
* generate data that constitutes a definition for the class. A typical strategy
* is to transform the name into a file name and then read a "class file" of
* that name from a file system.
*
*
@version
1.0, 07/04/17
*
@author
Zhao Jiucheng
*
*/
public
class
MyClassLoader
extends
ClassLoader
{
//
a classpath for search
private
static
String myClasspath
=
new
String(
""
);
//
hashtable that memory the loaded classes
private
static
Hashtable
<
String, Class
<?>>
loadClassHashTable
=
new
Hashtable
<
String, Class
<?>>
();
//
hashtable that memory the time of loading a class
private
static
Hashtable
<
String, Long
>
loadClassTime
=
new
Hashtable
<
String, Long
>
();
//
the null constructor
public
MyClassLoader()
{
}
/** */
/**
* create a classloader and specify a classpath.
*
*
@param
myClasspath
* the specified classpath name.
*/
public
MyClassLoader(String myClasspath)
{
if
(
!
myClasspath.endsWith(
"
\\
"
))
{
myClasspath
=
myClasspath
+
"
\\
"
;
}
MyClassLoader.myClasspath
=
myClasspath;
}
/** */
/**
* set the classpath
*
*
@param
myClasspath
* the specified classpath name
*/
public
void
SetmyClasspath(String myClasspath)
{
if
(
!
myClasspath.endsWith(
"
\\
"
))
{
myClasspath
=
myClasspath
+
"
\\
"
;
}
MyClassLoader.myClasspath
=
myClasspath;
}
/** */
/**
* Loads the class with the specified binary name. This method searches for
* classes in the same manner as the loadClass(String, boolean) method.
* Invoking this method is equivalent to invoking {loadClass(name,false)}.
*
*
@param
className
* The binary name of the class.
*
*
@return
The resulting <tt>Class</tt> object.
*
*
@throws
ClassNotFoundException
* If the class was not found.
*/
@SuppressWarnings(
"
unchecked
"
)
public
Class loadClass(String className)
throws
ClassNotFoundException
{
return
loadClass(className,
false
);
}
/** */
/**
* Loads the class with the specified binary name. The default
* implementation of this method searches for classes in the following
* order:
*
* Invoke {findLoadedClass(String)} to check if the class has already been
* loaded.
*
* Invoke {findSystemClass(String)} to load the system class.
*
* Invoke the {findClass(String)} method to find the class.
*
* If the class was found using the above steps, and the resolve flag is
* true, this method will then invoke the {resolveClass(Class)} method on
* the resulting Class object.
*
*
@param
name
* The binary name of the class.
*
*
@param
resolve
* If true then resolve the class.
*
*
@return
The resulting Class object.
*
*
@throws
ClassNotFoundException
* If the class could not be found.
*/
@SuppressWarnings(
"
unchecked
"
)
protected
Class loadClass(String name,
boolean
resolve)
throws
ClassNotFoundException
{
try
{
Class foundClass
=
findLoadedClass(name);
//
check if the class has already been loaded.
if
(foundClass
!=
null
)
{
System.out.println(
"
Complete to load the class:
"
+
name);
return
foundClass;
}
//
if the class is systemClass, load the system class by system
if
(name.startsWith(
"
java.
"
))
{
foundClass
=
findSystemClass(name);
loadClassHashTable.put(name, foundClass);
System.out.println(
"
System is loading the class:
"
+
name);
return
foundClass;
}
//
invoke the findClass() method to load the class
try
{
foundClass
=
findClass(name);
}
catch
(Exception fnfe)
{
}
if
(resolve
&&
(foundClass
!=
null
))
{
resolveClass(foundClass);
}
return
foundClass;
}
catch
(Exception e)
{
throw
new
ClassNotFoundException(e.toString());
}
}
/** */
/**
* Finds the class with the specified binary name.The default implementation
* throws a ClassNotFoundException.
*
*
@param
className
* The binary name of the class.
*
*
@return
The resulting Class object.
*
*
@throws
ClassNotFoundException
* If the class could not be found.
*/
@SuppressWarnings(
"
unchecked
"
)
public
Class findClass(String className)
{
byte
[] classData
=
null
;
try
{
classData
=
loadClassData(className);
}
catch
(IOException e)
{
e.printStackTrace();
}
if
( classData
==
null
)
{
return
null
;
}
System.out.println(
"
MyClassLoader is loading :
"
+
className
+
"
"
);
Class c
=
defineClass(className, classData,
0
, classData.length);
MyClassLoader.loadClassHashTable.put(className, c);
System.out.println(
"
Complete to load the class :
"
+
className);
return
c;
}
/** */
/**
* Loads the classData with the specified binary name. This method searches
* for classes in the specified classpath as
* searchFile(myClasspath,className) method.
*
*
@param
name
* The binary name of the class
*
*
@return
The resulting the classData of the class object by byte[]
*
*
@throws
IOException
* if have some failed or interrupted I/O operations.
*/
private
byte
[] loadClassData(String className)
throws
IOException
{
String filePath
=
searchFile(myClasspath, className
+
"
.class
"
);
if
(
!
(filePath
==
null
||
filePath
==
""
))
{
System.out.println(
"
It have found the file :
"
+
className
+
"
. Begin to read the data and load the class。
"
);
FileInputStream inFile
=
new
FileInputStream(filePath);
byte
[] classData
=
new
byte
[inFile.available()];
inFile.read(classData);
inFile.close();
loadClassTime.put(className,
new
File(filePath).lastModified());
return
classData;
}
else
{
filePath
=
searchFile(myClasspath, className
+
"
.java
"
);
if
(
!
(filePath
==
null
||
filePath
==
""
))
{
System.out.println(
"
It have found the file :
"
+
filePath
+
"
. Begin to translate
"
);
Runtime.getRuntime().exec(
"
javac
"
+
filePath);
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(
"
Translate it over :
"
+
filePath);
return
loadClassData(className);
}
else
{
System.out
.println(
"
Haven't found the file, and fail to read the classData!
"
);
return
null
;
}
}
}
/** */
/**
* Loads the class with the specified binary name.The default implementation
* throws a ClassNotFoundException.
*
*
@param
classData
* The data of the class.