javajavaccannot-find-symbol

Java - cannot find symbol. Both Files in same directory


I am new to java and have been trying to learn it and now I have been facing this error even though both the files are in the same folder :

BankTest.java:3: error: cannot find symbol
        BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
        ^
  symbol:   class BankAccount
  location: class BankTest
BankTest.java:3: error: cannot find symbol
        BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
                            ^
  symbol:   class BankAccount
  location: class BankTest
BankTest.java:12: error: cannot find symbol
            BankAccount c = new BankAccount( args[0], args[1] );
            ^
  symbol:   class BankAccount
  location: class BankTest
BankTest.java:12: error: cannot find symbol
            BankAccount c = new BankAccount( args[0], args[1] );
                                ^
  symbol:   class BankAccount
  location: class BankTest
BankTest.java:15: error: cannot find symbol
            BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
            ^
  symbol:   class BankAccount
  location: class BankTest
BankTest.java:15: error: cannot find symbol
            BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
                                ^
  symbol:   class BankAccount
  location: class BankTest

I have both the BankAccount.java and the BankTest.java in the same folder. I have tried downloading the source code and still face the same error.

javac BankAccount.java works perfectly but javac BankTest.java shows up the error. What am I doing wrong here?

This is the BankAccount.java

public class BankAccount {
    private String holderName;
    private double balance;
    private String number;

    public BankAccount( String holderName, String number ){
        this.holderName = holderName;
        this.number = number;
        balance = 0;
    }

    public BankAccount( String holderName, String number, double balance ){
        this.holderName = holderName;
        this.number = number;
        this.balance = balance;
    }

    public String getHolderName(){
        return holderName;
    }

    public void setName( String newName ){
        holderName = newName;
    }

    public void deposit( double amount ){
        balance += amount;
    }

    public void withdraw( double amount ){
        balance -= amount;
    }

    public double checkBalance(){
        return balance;
    }

    public String toString(){
        String s = number + "\t" + holderName + "\t" + balance;
        return s;
    }
}

And this is the BankTest.java:

public class BankTest {
    public static void main(String[] args) {
        BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
        System.out.println( b );
        b.deposit( 100 );
        System.out.println( b );
        b.withdraw( 500 );
        System.out.println( b );
        System.out.println( "Balance is: " + b.checkBalance() );

        if( args.length == 2 ){
            BankAccount c = new BankAccount( args[0], args[1] );
            System.out.println( c );
        } else {
            BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
            System.out.println( c );

        }

    }
}

Solution

  • You need to compile the classes, and have them on your classpath. A good way to manage this is to use the -d option. Make sure the directory "build" exists.

    javac -d build BankAccount.java
    

    That should compile and make a BankAccount.class in the "build" folder. Then you can do.

    javac -cp build -d build BankTest.java
    

    That should create the file BankTest.class in the build folder. Then you can run it.

    java -cp build BankTest
    

    You need to put the .class files on the classpath. That is why the -d option works well because you know what folder to put on the class path, it will also create directories for packages and put the class files in the correct location.