Results 1 to 7 of 7

Thread: Reflection

  1. #1
    akki is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default Reflection

    I hav written a simple code for finding out all the data fields of a class but i am getting an exceptio :"NoClassFoundException".....I am not understanding hoe to remove it...I would glad to receive any one's help....

    import java.lang.reflect.*;


    class SampleField
    { public static void getField(Class c)
    { //Class c= Class.forName(s);
    Field f[]=c.getFields();
    for(int i=0;i<f.length;i++)
    { System.out.println(" ");
    Class type=f[i].getType();
    System.out.print(type.getName());
    System.out.print(""+f[i].getName());
    }
    }

    public static void main(String... s1)
    { try{
    Class c= Class.forName(s1[0]);
    getField(c);
    }catch(Exception e){System.out.println(e);}
    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Add the class that was not found to the classpath when you run the program.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by akki View Post
    i am getting an exceptio :"NoClassFoundException"
    You removed the important part :)
    s1[0] = ???
    Which String do you pass to your programm? Is the ending .class? Remove it. Is the class in a package? Then write package+classname.

  4. #4
    akki is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    import java.lang.reflect.*;
    import java.awt.*;


    class SampleField
    { public static void getField(Class c)
    { //Class c= Class.forName(s);
    Field f[]=c.getFields();
    for(int i=0;i<f.length;i++)
    { System.out.println(" ");
    Class type=f[i].getType();
    System.out.print(type.getName());
    System.out.print(""+f[i].getName());
    }
    }

    public static void main(String... s1)
    { try{
    Class c= Class.forName(s1[0]);
    getField(c);
    }catch(Exception e){System.out.println(e);}
    }
    }



    I am sending a class name java.lang.thread through command line argument but still it is showing the same exception....:(

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Did you use the right case, Thread instead of thread?

  6. #6
    akki is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Did you use the right case, Thread instead of thread?
    Thanks Sir...I was doin the same mistake...:)

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. reflection
    By rfabbri in forum Advanced Java
    Replies: 3
    Last Post: 12-07-2010, 08:55 AM
  2. Generics and reflection
    By juza in forum Advanced Java
    Replies: 1
    Last Post: 11-30-2010, 03:48 PM
  3. how to use reflection
    By sunilpatel28 in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 10:51 PM
  4. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •