我在servlet使用了动态调用类,代码如下:              ClassLoader loader = this.getClass().getClassLoader();
              Class newPopedom = null ;
              QueryPopedom MyQueryPopedomClass = null;
              try{
                newPopedom = loader.loadClass(strPopedom);
                Class oldPopedom = loader.loadClass("qdservlet.query.QueryPopedom");
                System.out.println("get "+ strPopedom +" class ok!");
                MyQueryPopedomClass = (QueryPopedom)newPopedom.newInstance();
                if(oldPopedom.isInstance(MyQueryPopedomClass)==false){
                    
                    System.out.println( strPopedom + " is not a MultiSet class!");
                    strPopedomWhere = "";
                }else{
                    strPopedomWhere = MyQueryPopedomClass.getWhereString(setQueryGeneral.readString("strQueryID"),
                    setQueryGeneral.readString("strTableName"),setQueryGeneral.readString("strQueryName"),request,response) ;
                }
              }catch(ClassNotFoundException noEx){
               System.out.println( strPopedom + " don't finded ,strPopedomWhere is null .");
                     strPopedomWhere = "";
              }catch(Exception ex) {
                System.out.println("get new Instance error ,strPopedomWhere is null .");
                     strPopedomWhere = "";
             }

解决方案 »

  1.   

    pxq(风轻轻地吹) 
    呵呵,你的代码怎么这么累赘,我要的是动态的  从文件  重载一个同名的类文件。
      

  2.   

    重新加载时用一个新的ClassLoader
      

  3.   

    java.lang.Compiler可以动态编译
    从没试过,自己试一下吧
      

  4.   

    我昨天看错问题了,所以答非所问。我觉得你要再加载的话,需要自己写一个ClassLoader。
    因为系统的ClassLoader有缓存,加载一次以后就不会加载了。通过ClassLoader.loadClass方法源代码就可以知道。    protected synchronized Class loadClass(String s, boolean flag)
            throws ClassNotFoundException
        {
            Class class1 = findLoadedClass(s);
            if(class1 == null)
                try
                {
                    if(parent != null)
                        class1 = parent.loadClass(s, false);
                    else
                        class1 = findBootstrapClass0(s);
                }
                catch(ClassNotFoundException classnotfoundexception)
                {
                    class1 = findClass(s);
                }
            if(flag)
                resolveClass(class1);
            return class1;
        }