tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Cox, Charlie" <c...@cincom.com>
Subject RE: ClassCastException
Date Mon, 16 Jun 2003 13:46:02 GMT
you need to set your classloader's parent to the current classloader. Have
you reviewed Tomcat's classloader document so that you understand how tomcat
works before trying to load your own classes? 
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/class-loader-howto.html

Is there a reason to do this as opposed to moving your classes into tomcat's
WEB-INF directory? I wouldn't do this if you are just trying to avoid
copying your files to tomcat's directory from your network drive.

Charlie

> -----Original Message-----
> From: Anna [mailto:simonik@sturak.sk]
> Sent: Monday, June 16, 2003 9:15 AM
> To: Tomcat Users List
> Subject: ClassCastException
> 
> 
> 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;
>     }
> }
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message