/*
 * outclass.java
 *
 * Created on 2006年5月11日, 下午9:52
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */package 学校系统;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.security.*;
import javax.crypto.*;
/**
 *
 * @author new
 */
final public class outclass extends Thread{
    Socket s;//连接
    String path;//文件在机子中的绝对路径
    String file;//文件路径 文件名
    String classname;//类名
    String poststr;//运行参数
    URLClassLoader outload;//加载时所用的
    Class trueclass;//外部类loadclass时的对象
    /** Creates a new instance of outclass */
    public outclass(Socket s,String file,StringBuffer poststr) {
        System.out.println("启动外部程序");
        this.s=s;
        this.file=file;
        this.poststr=new String(poststr);
        classname=file.substring(file.lastIndexOf("\\")==-1?0:file.lastIndexOf("\\"));
        start();
    }
    public void run(){
        try{
            File f=new File(".");
            path=f.getAbsolutePath()+"/"+file;
            char[] pathchar=path.toCharArray();
            for(int i=0;i<pathchar.length;i++)
            {if(pathchar[i]=='\\')pathchar[i]='/';}
            path=new String(pathchar);
            URL[] outclass={new URL("file:///"+path)};//取得加载需要的类的URL对象
            outload=new URLClassLoader(outclass);//URL加载类对象
            classname=new String(classname.getBytes(),0,classname.indexOf("."));
            System.out.println("类名:"+classname);
            System.out.println("URL:"+path);
            trueclass=outload.loadClass(classname);
        }catch(Exception e){System.out.println("加载类失败"+e);return;}
    }//run
}//class
/*
每次都异常在这里产生trueclass=outload.loadClass(classname);
怎么解决!谢谢....
它老是说找不到类
*/

解决方案 »

  1.   

    classname=new String(classname.getBytes(),0,classname.indexOf("."));
    这个是什么意思
      

  2.   

    不用了吧!
    整个package拿上来,有点长!
    我只想知道trueclass=outload.loadClass(classname);
    为什么不对.classname是类名,还是类名.class 还是  包名.类名.class 还是 包名.类名
    或者是其它的什么!
    谢谢!
      

  3.   

    看不懂你在做什么。
    如果是要 load 文件,用 this.getClass().getResourceAsStream(), 或者直接用 FileInputStream.
    你想把一个文件整个 load 近来做成一个 class?