Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Convert File Object into Class Object

Thread view: 
Aned - 14 Sep 2007 12:24 GMT
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
Gordon Beaton - 14 Sep 2007 12:34 GMT
> 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

--
Roedy Green - 15 Sep 2007 20:37 GMT
>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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.