Convert File Object into Class Object

Discussion in 'Java' started by Aned, Sep 14, 2007.

  1. Aned

    Aned Guest

    is there any way to convert a java object of type File to type Class?
    my problem is that want to scan a directory and find files that ends
    with ".class" and treat them as classes. but the problem is that I
    only can retrive the files from a directory as File objects. I don't
    know if that is possible to tell my tool that if it finds a file with
    the extension ".class" then convert it to a Class object.

    many thanx
     
    Aned, Sep 14, 2007
    #1
    1. Advertising

  2. On Fri, 14 Sep 2007 11:24:37 -0000, Aned wrote:
    > is there any way to convert a java object of type File to type Class?


    That's what ClassLoaders do, but loadClass() maps from classname (not
    filename) to Class. That doesn't prevent you from writing your own
    though.

    /gordon

    --
     
    Gordon Beaton, Sep 14, 2007
    #2
    1. Advertising

  3. Aned

    Roedy Green Guest

    On Fri, 14 Sep 2007 11:24:37 -0000, Aned <> wrote,
    quoted or indirectly quoted someone who said :

    >is there any way to convert a java object of type File to type Class?
    >my problem is that want to scan a directory and find files that ends
    >with ".class" and treat them as classes. but the problem is that I
    >only can retrive the files from a directory as File objects. I don't
    >know if that is possible to tell my tool that if it finds a file with
    >the extension ".class" then convert it to a Class object.


    I suspect there are a number of fundamentals you have missed..

    1. a File object is just a file name description. It need not be
    associated with an actual file. You can create them from Strings with
    the File constructor. You can get a list of all the files in a
    directory with File.list. You can filter them in various ways so you
    just get the *.class or *.txt etc. by writing a FilenameFilter and
    using that with File.list. See http://mindprod.com/jgloss/file.html
    for details.

    It is highly unusual for newbies to do anything at all directly with
    *.class files. You just use classes and the JVM finds the associated
    *.class file, loads it and converts it to machine code, and does the
    static init.

    However it is possible do take control over class loading (convert
    *.class file to a Class object) either using Class.forName or by
    writing a ClassLoader. These are relatively advanced things to do. See
    http://mindprod.com/jgloss/classforname.html
    http://mindprod.com/jgloss/classloader.html

    However, a class file is just a mess of bytes, just like any other
    file, so you can read it just as you can any other file. You might
    use DataInputStream. See http://mindprod.com/applet/fileio.html
    for crude experimenting.
    See http://mindprod.com/jgloss/bcel.html if you want to read and
    analyse the class file byte for byte.

    If the class file is inside a zip, you can read it as a resource.
    See http://mindprod.com/jgloss/resource.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prakash
    Replies:
    2
    Views:
    2,090
    Prakash
    May 12, 2004
  2. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  3. Harry Zoroc
    Replies:
    1
    Views:
    948
    Gregory Vaughan
    Jul 12, 2004
  4. G
    Replies:
    16
    Views:
    3,119
  5. MoshiachNow

    convert a bitmap file into a jpg file ?

    MoshiachNow, May 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    94
    MoshiachNow
    May 3, 2007
Loading...

Share This Page