473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

class cast exception when I tried to covert the class object to specific one

2
Hi,

In my custom classloader i read the bytes using fileinputstream and constructed a class object using define class method.

then
I have used newInstance method on that loaded class object.
But casting to the specific Object is failing with class cast exception
can any one let me know the reason

regards
shanawaz
Jan 22 '08 #1
4 1795
dmjpro
2,476 2GB
Hi,

In my custom classloader i read the bytes using fileinputstream and constructed a class object using define class method.

then
I have used newInstance method on that loaded class object.
But casting to the specific Object is failing with class cast exception
can any one let me know the reason

regards
shanawaz
Show me the sample Code.


Debasis Jana
Jan 22 '08 #2
shanu
2
Show me the sample Code.


Debasis Jana
Hi Debasis,

Expand|Select|Wrap|Line Numbers
  1. class Loader1 extends ClassLoader{
  2.   public Class findClass(String name) throws ClassNotFoundException{
  3.     System.out.println("in findClass");
  4.     byte[] b=null;
  5.    try{
  6.      b = loadClassData(name);
  7.    }catch(Exception e){
  8.  
  9.    }
  10.    Class c=defineClass("javaexp.PrimeNumber",b,0,b.length);
  11.   // resolveClass(c);
  12.    //        Compiler.compileClass(c);
  13.    try{
  14.      Constructor con=c.getConstructor(new Class[]{});
  15.      System.out.println(con.newInstance(new Object[]{}));
  16.        javaexp.PrimeNumbe ov1=(javaexp.PrimeNumbe)con.newInstance(new Object[]{});//exception comes here
  17.      //javaexp.PrimeNumbe ov = (javaexp.PrimeNumbe) c.newInstance();
  18.    }catch(Exception e){
  19.      e.printStackTrace();
  20.    }
  21.      return c;
  22.   }
  23.  
  24.   private byte[] loadClassData(String name) throws FileNotFoundException{
  25.     System.out.println("in loadClass");
  26.     File f=new File(name);
  27.     FileInputStream fi=new FileInputStream(f);
  28.     BufferedInputStream bi=new BufferedInputStream(fi);
  29.     byte  b[]=null;
  30.     //int b;
  31.     try{
  32.     b=new byte[bi.available()];
  33.     bi.read(b);
  34.     bi.close();
  35.     fi.close();
  36.     }catch(Exception e){
  37.       e.printStackTrace();
  38.     }
  39.  
  40.     return b;
  41.   }
  42. }
  43.  
  44. public class MyClassLoader {
  45.   public MyClassLoader() {
  46.   }
  47.   public static void main(String args[])throws Exception{
  48.     Loader1 l1=new Loader1();
  49.  
  50.     System.out.println("before calling");
  51.     Class c=l1.loadClass("C:/cptbackup/DM/classes/javaexp/PrimeNumber.class");
  52.  
  53.     //Class c=l1.loadClass("javaexp.Override");
  54.     //Class c=Class.forName("javaexp.Override");
  55.     if(c.newInstance() instanceof Class){
  56.       System.out.println("yes");
  57.     }
  58.  
  59.    // Override ov=(Override)c.newInstance();
  60.    //Constructor con=c.getConstructor(new Class[]{});
  61.     //   Object com=(Object)con.newInstance(new Object[]{});
  62.    // javaexp.Override om=(javaexp.Override)ov;
  63.  
  64.    // if(ov instanceof Override){
  65.      // System.out.println("yes");
  66.     //}
  67.  
  68.  
  69.   }
  70. }
Thanks & regards
shanawaz shaik
Jan 23 '08 #3
BigDaddyLH
1,216 Expert 1GB
javaexp.PrimeNumbe ov1=(javaexp.PrimeNumbe)con.newInstance(new Object[]{});//exception comes here
What happens if you debug this line? If you do this?

Expand|Select|Wrap|Line Numbers
  1. Object object = con.newInstance(new Object[]{});
  2. System.out.println(object.getClass());
Jan 23 '08 #4
RedSon
5,000 Expert 4TB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Jan 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: ahaupt | last post by:
Hi all, I'm currently writing a load of class libraries, but not the main application iteslf. I want to provide some method for reporting errors back to the main application. At the moment...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
12
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.