By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

Reflection

P: n/a
Hi for everyone, i'm trying to make reflection to a .class or a .java
java file, anybody know how can i obtain a Class instance from a file?
Thks and regards,
Alejandro
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Alejandro Narancio wrote:
Hi for everyone, i'm trying to make reflection to a .class or a .java
java file, anybody know how can i obtain a Class instance from a file?
Thks and regards,
Alejandro


If you have a .class file, the easiest way is to make sure that the
..class file is in the proper location in the CLASSPATH. Then the
default system class loader will find the class and you can just use it.
If the name of the class is determined dynamically, use the
Class.forName() method. This technique is employed to load JDBC drivers,
for example.

If it is not possible to include the .class file in the CLASSPATH, you
need to write a custom ClassLoader. Read the file into a byte array and
invoke the ClassLoader.defineClass() method.

If all you have is a .java file, you need to invoke some sort of
compiler (AFAIK, a compiler is not included with the JRE) to create a
..class file and use one of the above techniques.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.