• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Dynamically calling methods

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a snippet of code I borrowed from rgagnon.com to dynamically call methods from a specialized error class. My calling method is as follows:

Would I be correct to assume that my class name String needs to be a fuill path or something? I am generating ClassNotFoundExceptions. I have tried "Error", "error", "../Error", "Error.class", etc. Apparently I am not doing somethig right with the pathname, but I am at a bit of a loss...

At any rate, I had a good time looking up "java Class class"..
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where you assign aClass to "Error," try the fully qualified name there,You will just move your exception to the getDeclaredMethod() call.

I am not sure what to do about that, but I don't think your empty Class[] array is what the method call wants . . .
I am not sure what you want in params[] but I think it means that if your method takes (int a, double b, String c) you should put {int, double, String} as your param[] array.


I am probably mistaken about that, but try it anyway and see what happens
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the full path name, but still generated the Class Not Found Error. (Believe it or not, I had not yet tried that).

If I am understanding the API correctly (and the sample code), since my constructor requires no arguments, I am ok by not declaring any parameters. Incidentally, the code that I borrowed was from here. Maybe someone will see something that I am not.

I suppose the easy path would be to just declare an instance of my error class and call the methods directly, but that hardly seems as fun And I am not so sure that with my current implementation that would even work.
[ November 27, 2005: Message edited by: C Clites ]
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by "the full path name" you meant something like "C:/My Documents/Java/banana/error.class" then no, you don't do that. You should put the full name of the class, which is the package name plus a dot plus the class name. So if your code looks like this:then the full name of the class is "org.wasabi.SomeError".
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your solution is in Campbell Ritchie's reply. Try it rather than saying


(Believe it or not, I had not yet tried that).



If in doubt check the api for Class here

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html
[ November 28, 2005: Message edited by: jiju ka ]
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point with that comment was that sometimes even when we think we have tried everything, we forget the simple things ( I see how that may have escaped you though ) . But I was able to implement the suggestions, and my code works fine (at least that part of it does ).

Thanks for your concern.. I would have never known how to look up a java API..

(But seriously, thank you Campbell Ritchie and Paul Clapham ).
[ November 28, 2005: Message edited by: C Clites ]
 
Campbell Ritchie
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!