2

Possible Duplicate:
ClassnotFound exception using java reflection

I get exception, java.lang.ClassNotFoundException: SavingAccount, while executing the following code:

public class AccountFactory {   
    public void getAccount()
    {
        IAccount account;
        account = null;
     try
        {
         account = (IAccount)Class.forName("SavingAccount").newInstance();
         account.Deposit(500);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }  
    }

}

What could be the possible reasons causing the error?

this is my saving account code:

public class SavingAccount implements IAccount {
    int money;
    SavingAccount()
    {
        money =0;
    }
    public void WithDraw(int m)
    {
        money--;
    }
    public void Deposit(int m)
    {
        money++;
        System.out.println(money);
    }
}
9
  • 2
    Please avoid using chat language. Commented Sep 23, 2012 at 9:52
  • 1
    Why write (IAccount)Class.forName("SavingAccount").newInstance()? Why not write new SavingAccount() instead? Commented Sep 23, 2012 at 9:52
  • i want to dynamically assign the class name, like savingAccount Commented Sep 23, 2012 at 9:54
  • @fatima Can you show SavingAccount class? Commented Sep 23, 2012 at 9:57
  • But the possible class names you must be knowing at compile time right?? Commented Sep 23, 2012 at 9:57

5 Answers 5

4

The classloader cannot find the SavingsAccount class. You also need to use the fully qualified name for the class when using the Class.forName method as specified in the Java API. A fully qualified class name includes the class name prefixed by the package structure. If your AccountFactory class is always going to create a class of type SavingsAccount every time, I would recommend not even having an AccountFactory class and instead just using:

IAccount account = new SavingsAccount();

If the posted code is just a snapshot of your class and you do intend to return different types implementing the IAccount interface from your factory you will want to change the getAccount method signature so that it returns an IAccount and not void. You must then use the return statement to return an object that implements the IAccount interface. Such as:

 public IAccount getAccount()
 {
     IAccount account;
     account = null;
     try
        {
         //Notice fully qualified name is used.
         account = (IAccount)Class.forName("org.mydomain.SavingAccount").newInstance();
         account.Deposit(500);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        } 
     return account;
}
Sign up to request clarification or add additional context in comments.

Comments

1

write

public class AccountFactory {   
    public void getAccount()
    {
        IAccount account;
        account = null;
     try
        {
         account = new SavingAccount();
         account.Deposit(500);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }  
    }        
}

and follow compile error.

  1. You probably see why SavingAccount not found (not in class path? wrong package (i.e. "mypackage.SavingAccount"?))
  2. You see if SavingAccount implement IAccount imterface.

Comments

1

Try specifying fully qualified class names . Like

Class.forName("abc.xyz.SavingAccount");

Where abc.xyz is the package name for SavingAccount class.

Comments

1

Your AccountFactory must have been over IAccount. I think it is used to return the instance of all the implementation of IAccount based on the requiement.. But, I assume that you know what all classes are implementing your IAccount. So, there is no need to use Class.forName().

I assume your design is like the one given below: -

public interface IAccount {
}

public class SavingsAccount implements IAccount {
}

public class CurrentAccount implements IAccount {

}

public class AccountFactory {
     public static IAccount getAccountInstance(String class) {   
     // In your code you need to change the return type of this method..

          if (class.equals("Savings")) {
                 return new SavingAccounts();
          } else if (class.equals("Current")) {
                 return new CurrentAccount();
          } 
          // Similarly for Other implementor....
     }
}

Comments

0

It shows that SavingAccount class not on the classpath.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.