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

Dynamic Class loading

 
Kathy Thiyagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kathy, Welcome to javaranch

It will be easy if you give code for class which String "type" is stored
 
Kathy Thiyagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually when I created file then the program runs fine.
I think you had not write Constructor properly.
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pramode. You are correct. It makes sense. Once again, thank you so much. Take care.

Regards,

Kathy
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are most welcome
 
Kathy Thiyagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Kathy Thiyagar : *use code tag*, when you post code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic