I have one class which takes classname in runtime and creates instance of that class. I have done this with reflection. Please see my code below
//Interface
   public interface DDInterface {
     public void process();
     }
// class which implements the interface
   package com.dd.august;
   public class DDClass implements DDInterface 
       {
       public void process()
        {
        System.out.println("Hello All");
         }
       }
//the main class [This works fine if I hard code inside Class.forName()] - scenario 1
        DDInterface ddInterface = 
           Class.forName("com.dd.august.DDClass").newInstance();
          ddInterface.process();
// But this way it does not work if I pass classname dynamically - scenario 2
        String className="com.dd.august.DDClass";
         DDInterface ddInterface = 
           Class.forName(className).newInstance();
          ddInterface.process();
The error I am getting from this is java.lang.ClassNotFoundException: com/dd/august/DDClass. All the 'packagename.classname' is changing in 'packagename/classname' which doesn't happen in scenario 1.
Anyone know how to resolve this?
Regards, Sharmi
Aucun commentaire:
Enregistrer un commentaire