This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Dynamic Class loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamic Class loading" Watch "Dynamic Class loading" New topic
Author

Dynamic Class loading

Kathy Thiyagar
Greenhorn

Joined: Mar 30, 2010
Posts: 9
Hi,

I am trying to read a file and create a new instance of the classes whenever it finds the class name from this file and would like to know how to get the Constructor. My code works fine, but when it reaches the Constructor line, it throws ClassNotFoundException. I have all of the required classes compiled in the same folder. Looks like, I am doing something silly here. Can someone point me in the right direction? I am new to this Reflection stuffs.


Thanks in advance.

Kathy T.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hello Kathy, Welcome to javaranch

It will be easy if you give code for class which String "type" is stored


Life is easy because we write the source code.....
Kathy Thiyagar
Greenhorn

Joined: Mar 30, 2010
Posts: 9
Hi Pramod,

Thank you for your quick reply. The file that is loading is as follows.

TestClassA : 0
TestClassB : 1

You can create a simple TestClassA and TestClassB and compile them under the test folder. The whole program is really big and for demo, I have only listed partial code here. This code compiles without any issues except this is not getting the Constructor.

Thanks,

Kathy
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Actually when I created file then the program runs fine.
I think you had not write Constructor properly.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

when I created file Config.txt and in that I had write "Code:123".

In Code class I had following code



when I run program it print


But when I removed the constructor then it will give error as



So it must not getting the constructor which takes argument as(Code c, long l) .

so check your constructor in that class.
Kathy Thiyagar
Greenhorn

Joined: Mar 30, 2010
Posts: 9
Thanks Pramode. You are correct. It makes sense. Once again, thank you so much. Take care.

Regards,

Kathy
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

You are most welcome
Kathy Thiyagar
Greenhorn

Joined: Mar 30, 2010
Posts: 9
Pramode, I have corrected my code and it is working like charm. I have been spending few hours trying to figure out what I am doing wrong here. You are really a Java Guru.

Thanks,

Regards,

Kathy.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Here on ranch there are many talented progammer. I am just no one compare to them. It is good to hear that your program run fines.
Best Luck
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

@Kathy Thiyagar : *use code tag*, when you post code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Class loading