Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading a ".class" from a specified path using a JAVA program

 
Amol Parikh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to compile a JAVA file that i have stored at a location.After compilation a .class is created in the same directory. I would like to load this class file in the memory and fiddle around with the reflection API. Would be grateful for your inputs...
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You make sure the class is on the classpath of the program that is doing the reflection and load it using Class.forName.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's Java and not JAVA (not an acronym) and welcome to the Ranch, Amol.
 
Amol Parikh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not on the class path and would like to load it explicitly using an absolute path...!!!
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works for java.util.List class as well as for the Animal one -

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amol Parikh wrote:It is not on the class path and would like to load it explicitly using an absolute path...!!!


Then you'll need to create a class loader and define the class, see :http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic