-2

How to Creating an instance of a class from string i.e.class name will be passed as string and also want to call methods (methods do not have any parameters).

for example with class name TEST having methods like 1) GetData 2) GetAddtionof2numbres etc...

i want to pass class name TEST using a variable.

  String className = "TEST";

i have tried with refection i.e using as below

Class cls = Class.forName(className);
object o = cls.newinstance();

but not able to get/access methods in that class.

when i get method names from the class i will compare them with the names stores in text files and run that method in that class by creating an instance of the class at run time.

Can any body help me in this

thanks in advance

i have tried with below

String className = "test.t456"; 
Class c = Class.forName(className);
Object obj = c.newInstance();
Method[] method = c.getDeclaredMethods();

        for(Method m : method)
        {
            System.out.println(m.getName());

        }

able to print method names in console. Now how to run these methods by creating instance of that class.?

Anji R
  • 843
  • 3
  • 16
  • 23
  • Isnt that an **O** bject in the last line of codez? – jcklie Dec 19 '13 at 18:28
  • and the method is newInstance(). – jalynn2 Dec 19 '13 at 18:30
  • 1
    And 'but not able to get/access methods in that clas' means? The call should have thrown an exception, please show the stacktrace... – home Dec 19 '13 at 18:30
  • Start by taking a look at [this question](http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string) on how to open methods via reflection. – hineroptera Dec 19 '13 at 18:37
  • `Method[] methods = cls.getDeclaredMethods();` - this will get you what you want – Алексей Dec 19 '13 at 18:41
  • i have tried with below String className = "test.t456"; Class c = Class.forName(className); Object obj = c.newInstance(); Method[] method = c.getDeclaredMethods(); for(Method m : method) { System.out.println(m.getName()); } able to print method names in console. Now how to run these methods by creating instance of that class.? – Anji R Dec 19 '13 at 18:45

1 Answers1

0

I would suggest you first read this.

However to answer you question, you can do something like this:

String className = <Your class name>; 
        try {
            Class c = Class.forName(className);
            Object obj = c.newInstance();
            Method methodToInvoke = obj.getClass().getMethod(<your method name>, <your method arg types>);
            methodToInvoke.invoke(obj, <arguments>);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Алексей
  • 1,847
  • 12
  • 15
  • thanks for the reply, but this is getting package name for the class it belongs to , if i want to get all the packages in that particular project i.e for example in a project we have Package1,Package2 ...packagen in this scenario how to get all the packages once again thank you for your help – Anji R Dec 20 '13 at 01:45