0

I want to access one or many classes from an entry point(main) without importing package. For example:

package com.ank.dynamicJarFileCreation;

public class revDemo {
    public static int reverseNumber(int n)
     {
         int rem,rev=0;
         while(n>0)
         {
             rem=n%10;
             rev=rev*10 + rem;
             n=n/10;
         }
         return rev;
     }
}

above one is revDemo class and I want to access above reverseNumber(int n) method from an entry point below. For example:

public class revCall {

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

    int n=485154;

    System.out.println(revDemo.reverseNumber(n));

Or by this way.

 Class cls = Class.forName("reverseDemo");
        Object obj =  cls.newInstance();

        System.out.print("Class Name"+cls.getName());
        Object obj =  null; //It is a object of -> com.ank.dynamicJarFileCreationo

        System.out.println(((com.ank.dynamicJarFileCreation)obj).reverseNumber(n));

    }

}
S.I.
  • 3,250
  • 12
  • 48
  • 77

1 Answers1

0

You are very near to your own solution. Actually you created the instance of your required class dynamically using Class.forName() and newInstance() functions.

Next step is to get the Function from the Class by the name of method, say reverseNumber and execute the same using created instance of revDemo .

Class revDemoClazz = Class.forName("com.ank.dynamicJarFileCreation.revDemo");
Object revDemoObj = revDemoClazz.newInstance();
Method reverseNumberMethod = revDemoClazz.getMethod("reverseNumber", Integer.class);
reverseNumberMethod.invoke(revDemoObj, 12345);
Anil Agrawal
  • 2,748
  • 1
  • 24
  • 31
  • I did accordingly but getting Exception like `Exception in thread "main" java.lang.NoSuchMethodException: com.ank.dynamicJarFileCreation.reverseNumber(java.lang.Integer) at java.lang.Class.getMethod(Unknown Source) at reverseCallerFun.main(revCall.java:16)` – ANKIT RAMAN Jan 26 '17 at 07:02
  • As I can see your actual method signature is like public static int reverseNumber(int n); So to find that method you need get method as revDemoClazz.getMethod("reverseNumber", int.class) – Anil Agrawal Jan 26 '17 at 08:57
  • See the difference at one place we are passing Integer.class and at other int.class as a parameter to method getMethod() – Anil Agrawal Jan 26 '17 at 08:59
  • Yes,I correct it like your way and now there is no any Exception but still it is not doing reverse. Also let me know if there any way to call non static method. – ANKIT RAMAN Jan 26 '17 at 09:06
  • At first argument of function Method.invoke() pass the object of target object, and the non-static method of target object will be invoked. – Anil Agrawal Jan 26 '17 at 09:22
  • your welcome :-) If your problem is resolved please accept the answer. – Anil Agrawal Jan 26 '17 at 15:30
  • As I am new here,when accepting your answers I am seeing pop up comments like: Votes cast by those who has less than 15 reputations are recorded but do not change the publicly displayed post score . – ANKIT RAMAN Jan 27 '17 at 04:02
  • How do I compile Hadoop hbase LZO Compression Decompression Program in Java? Kindly explain. – ANKIT RAMAN Jan 28 '17 at 07:52
  • How do I compile Hadoop hbase LZO Compression Decompression Program in Java on windows ? – ANKIT RAMAN Jan 28 '17 at 08:00
  • Its better to create a new question. It will attract other users too. – Anil Agrawal Jan 28 '17 at 14:51