keyongtech


  keyongtech > java > 09/2007

 #1  
09-14-07, 12:24 PM
Aned
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
 #2  
09-14-07, 12:34 PM
Gordon Beaton
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

--
 #3  
09-15-07, 08:37 PM
Roedy Green
On Fri, 14 Sep 2007 11:24:37 -0000, Aned <basem415> 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
Similar Threads
why should base class object points to derived class object (virtualfunction question) ?

While trying to understand the working principle of virtual functions, I asked myself that why in this world do we need virtual functions at the first place ? Why do base...

Way to convert List object to class object

hi I was trying to acces a class using a List object in a different class. I want the object to have the same features as LIst object but the same time have acces to a...

Convert object GUID to schema class name

I'm writing an application which looks at Active Directory permissions. For each ACL, I have the GUID of the object the permissions apply to. How can I convert the GUID into...

Question regarding initializing base class object to derived class object,

Hi all, I am confuse in concept of inheritance. I am having following 2 classes class base1 { public void abc() { System.Console.WriteLine("base1 abc"); }

Cannot convert from object class to object

Hi All, Probably a silly question to those who know, but i'm a noob with C# :D I have a COM component written in VB6 I'm trying to interop to from C# (using the IDE 'Add...


All times are GMT. The time now is 11:24 PM. | Privacy Policy