• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Class.forName()

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys, I have a problem with Class.forName()

In a method of a class I have a string variable which stores a class name like this..

void myMethod() {
String str1 = "com.matrix.heman";
}

In the above myMethod I need to create an object of the class "com.matrix.heman" and invoke the main method of it by passing some arguments. I did like this.. but i didnt worked

trail1
-------

void myMethod() {
String str1 = "com.matrix.heman";
Class myClass = Class.forName(str1);
myClass.main(args);
}

trail2
------

void myMethod(){
String str1 = "com.matrix.heman";
Class myClass = Class.forName(str1);
myClass obj = new myClass();
obj.main();
}


 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Please UseCodeTags in the future.
2)

but i didnt worked


ItDoesntWorkIsUseless.

That said, of course neither works. Class doesn't have a method called main. Neither does Object. What you need is to use a little bit more reflection. Use myClass.getMethod(???) to get the main method, then invoke it. I'm sure you'll be able to fill in the question marks. One hint: String[].class returns you the Class object that represents String arrays.
 
Hemanth Kumar Simhadri
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:1) Please UseCodeTags in the future.

ItDoesntWorkIsUseless.

That said, of course neither works. Class doesn't have a method called main. Neither does Object. What you need is to use a little bit more reflection. Use myClass.getMethod(???) to get the main method, then invoke it. I'm sure you'll be able to fill in the question marks. One hint: String[].class returns you the Class object that represents String arrays.




I did this way. It still didnt worked. Please help with the .getMethod(String, Class[]); method. Not able to implement this


Class myClass = class.forName("com.matrix.heman");
Method myMethod = myClass.getMethod("main");
myMethod.invoke(args);

Please help in getting the solution.

not able to implement the getMethod() properly
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check whether the following link is useful

webpage
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hemanth Kumar Simhadri wrote:I did this way. It still didnt worked. Please help with the .getMethod(String, Class[]); method. Not able to implement this


Class myClass = class.forName("com.matrix.heman");
Method myMethod = myClass.getMethod("main");
myMethod.invoke(args);

Please help in getting the solution.

not able to implement the getMethod() properly


What does getMethod take as parameters? The Javadoc clearly states: 1) the method name, 2) the method parameter types. You are trying to get method "main()" -- without parameters. You'll need to pass the class for String[], and I've already shown you how to get that.

Also take a good look at the invoke method, and especially what the first argument should be.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic