tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anna" <simo...@sturak.sk>
Subject ClassCastException
Date Mon, 16 Jun 2003 13:14:52 GMT
i solved problem with NoClassDefFoundError

but i have other problem

This program will use SimpleClassLoader.
        >>>>>> Load class : TestClass
        >>>>>> Not a system class.
        >>>>>> Load class : test.LocalModule
        >>>>>> Not a system class.
        >>>>>> Load class : java.lang.Object
        >>>>>> returning system class (in CLASSPATH).
        >>>>>> Returning newly loaded class.
        >>>>>> Returning newly loaded class.
        >>>>>> Load class : java.util.Vector
        >>>>>> returning system class (in CLASSPATH).
Caught exception : java.lang.ClassCastException



########################## myjsp.jsp #########################

    SimpleClassLoader sc = new SimpleClassLoader("M:\\Java\\JSP Projects\\Ticker\\servlet4\\");
       Object o;
       String tst = "TestClass";

       System.out.println("This program will use SimpleClassLoader.");
           try {
               Class cClass = sc.loadClass(tst);
              o= cClass.newInstance();
    

///            here ... throw  cast exception ... in debuger i see that in "o" is instance
of TestClass (TestClass is implented class of interface LocalModule)

          ((LocalModule)o).start("DF");
        

           } catch (Exception e) {
               System.out.println("Caught exception : "+e);
          }




#############################

package test;

public interface LocalModule {
    /* Start the module */
    void start(String option);
}


#############################
I'm using this classloader


/**
 * Created by IntelliJ IDEA.
 * User: Alknaion
 * Date: 16.6.2003
 * Time: 12:23:54
 * To change this template use Options | File Templates.
 */


package test;
import java.util.*;
import java.io.*;

public class SimpleClassLoader extends ClassLoader {
    private Hashtable classes = new Hashtable();
    private String basePath;

    public SimpleClassLoader(String basePath) {
     this.basePath = basePath;
    }


   private byte[] getTypeFromBasePath(String typeName) {

  FileInputStream fis;
  String fileName = basePath + File.separatorChar
          + typeName.replace('.', File.separatorChar)
          + ".class";

  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();
 }

    public Class loadClass(String className) throws ClassNotFoundException {
        return (loadClass(className, true));
    }

    public synchronized Class loadClass(String className, boolean resolveIt)
     throws ClassNotFoundException {
        Class result;
        byte  classData[];

        System.out.println("        >>>>>> Load class : "+className);

        result = (Class)classes.get(className);
        if (result != null) {
            System.out.println("        >>>>>> returning cached result.");
            return result;
        }

        try {
            result = super.findSystemClass(className);
            System.out.println("        >>>>>> returning system class (in
CLASSPATH).");
            return result;
        } catch (ClassNotFoundException e) {
            System.out.println("        >>>>>> Not a system class.");
        }

        /* Try to load it from our repository */
        classData = getTypeFromBasePath(className);
        if (classData == null) {
            throw new ClassNotFoundException();
        }

        /* Define it (parse the class file) */
        result = defineClass(className, classData, 0, classData.length);
        if (result == null) {
            throw new ClassFormatError();
        }

        if (resolveIt) {
            resolveClass(result);
        }

        classes.put(className, result);
        System.out.println("        >>>>>> Returning newly loaded class.");
        return result;
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message