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