Question

I'm new to programming with Java, and I am attempting to write a program that will read in client data for a fictional bank, use polymorphism to calculate the interest based on the type of account (business, checking, savings). It compiling properly, and when I run it, it seems to be working fine for business accounts, but when it gets to the checking and savings accounts, I get a NoClassDefFoundError.

Okay, here's the relevant code:

Here is the call to the class constructor for the class that isn't working (j being determined by a for loop):

client[j]=new savings_accounts(name, account_number, phone, ssn, balance, type);

The instruction in main to calculate the closing balance:

client[j].close_balance=client[j].closing();

And here is the class that isn't working:

class savings_accounts extends account
{
  public savings_accounts(String name, int account_number, String phone_number,
                 String ssn, int open_balance, String acct_type){
    super(name, account_number, phone_number, ssn, open_balance, acct_type);
    open=open_balance;
  }
  int open;
  public float close_balance;
  public float closing(){
    float close;
    if(open<5000) close=(float)open*1.04;
    else close=(float)open*1.05;
    return close;
  }}

Thanks for the help!

Was it helpful?

Solution

There are two common reasons for NoClassDefFoundError:

  1. The version of the class you (or one of the packages you're using) compiled with is not the version you're running with. This can be due to simply needing to recompile all your stuff, or due to having the wrong version of a 3rd-party JAR file in your classpath.
  2. You have a class that's located in x/y/z/MyClass.class, and is supposed to be in package x.y.z, but you left out (or improperly coded) the package statement when you compiled.

There are also a bunch of "uncommon" reasons -- NoClassDefFoundError is the "garbage" exception for something being wrong in class loading.

OTHER TIPS

NoClassDefFoundError

This isn't the same as ClassNotFoundException. It usually means that a file was found in the expected place, but it didn't contain the expected class, i.e. wrong name or wrong package. A clean build usually fixes it, otherwise check that your class name matches the file name exactly, and that the package name agrees exactly with the directory hierarchy the source file is in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top