• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Class.forName()  RSS feed

 
Hemanth Kumar Simhadri
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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();
}


 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check whether the following link is useful

webpage
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!