实现了一个自定义的ClassLoader,代码如下:package ClassLoader;import java.io.*;
import hello.NewI;
import hello.HelloWorld;public class New2ClassLoader extends ClassLoader{ private String basePath;
public New2ClassLoader() {
super();
}
public New2ClassLoader(ClassLoader parenet, String basePath) {
super(parenet);
this.basePath = basePath;
}
public New2ClassLoader(String basePath) {
this.basePath = basePath;
}
/**
* @param className
*/
protected Class findClass(String className)
throws ClassNotFoundException
{
byte classData[];
classData = getTypeFromBasePath(className);
if(classData==null)
{
throw new ClassNotFoundException();
}
System.out.print(classData.length);
return defineClass(className , classData,0, classData.length);
}
/**
* @param typename
*/
private byte[] getTypeFromBasePath(String typeName)
{
FileInputStream fis ;
String fileName = basePath + File.separatorChar
+ typeName.replace('.',File.separatorChar)
+".class";
System.out.print(fileName);
try{
fis = new FileInputStream(fileName);
}
catch(FileNotFoundException e)
{
return null;
}
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
int c = bis.read();
while(c!=-1)
{
out.write(c);
c = bis.read();
}
}catch(IOException e)
{
return null;
}
return out.toByteArray();
}
}然后,采用如下的代码调用这个ClassLoader:
import ClassLoader.New2ClassLoader;
import hello.NewI;
import hello.HelloWorld;
//import hello.NewI;
import ClassLoader.AnotherClassLoader;
public class TestLoader { public TestLoader() {
super();
// TODO Auto-generated constructor stub
} /**
* @param args
*/
public static void main(String[] args) {
New2ClassLoader l = new New2ClassLoader(null,"D:");
try{
Class c = l.loadClass("hello.HelloWorld");
Object o = c.newInstance();
Helloworld h = (Helloworld) o;
h.Hello();
}
catch(Exception e)
{
e.printStackTrace();
}
}}
运行到 Helloworld h = (Helloworld) o;这句话的时候就会抛出java.lang.ClassCastException异常。请问这是什么问题呢?
import hello.NewI;
import hello.HelloWorld;public class New2ClassLoader extends ClassLoader{ private String basePath;
public New2ClassLoader() {
super();
}
public New2ClassLoader(ClassLoader parenet, String basePath) {
super(parenet);
this.basePath = basePath;
}
public New2ClassLoader(String basePath) {
this.basePath = basePath;
}
/**
* @param className
*/
protected Class findClass(String className)
throws ClassNotFoundException
{
byte classData[];
classData = getTypeFromBasePath(className);
if(classData==null)
{
throw new ClassNotFoundException();
}
System.out.print(classData.length);
return defineClass(className , classData,0, classData.length);
}
/**
* @param typename
*/
private byte[] getTypeFromBasePath(String typeName)
{
FileInputStream fis ;
String fileName = basePath + File.separatorChar
+ typeName.replace('.',File.separatorChar)
+".class";
System.out.print(fileName);
try{
fis = new FileInputStream(fileName);
}
catch(FileNotFoundException e)
{
return null;
}
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
int c = bis.read();
while(c!=-1)
{
out.write(c);
c = bis.read();
}
}catch(IOException e)
{
return null;
}
return out.toByteArray();
}
}然后,采用如下的代码调用这个ClassLoader:
import ClassLoader.New2ClassLoader;
import hello.NewI;
import hello.HelloWorld;
//import hello.NewI;
import ClassLoader.AnotherClassLoader;
public class TestLoader { public TestLoader() {
super();
// TODO Auto-generated constructor stub
} /**
* @param args
*/
public static void main(String[] args) {
New2ClassLoader l = new New2ClassLoader(null,"D:");
try{
Class c = l.loadClass("hello.HelloWorld");
Object o = c.newInstance();
Helloworld h = (Helloworld) o;
h.Hello();
}
catch(Exception e)
{
e.printStackTrace();
}
}}
运行到 Helloworld h = (Helloworld) o;这句话的时候就会抛出java.lang.ClassCastException异常。请问这是什么问题呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货