0

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 );

        }

    }
}
Dev
  • 55
  • 1
  • 1
  • 5
  • is BankAccount compiled?µ – Stultuske Feb 16 '23 at 10:00
  • It's not always clear when javac will grab other source files. `BankAccount` does not appear to depend on anything else, so `javac BankAccount.java` should work and produce `BankAccount.class`. To compile both files at once, `javac BankAccount.java BankTest.java` – matt Feb 16 '23 at 10:12
  • javac BankAccount.java works perfectly but javac BankTest.java shows up the error. javac BankAccount.java BankTest.java also seem to run fine but the error pops up when I try to run BankTeset.java – Dev Feb 16 '23 at 10:17

1 Answers1

1

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.

matt
  • 10,892
  • 3
  • 22
  • 34
  • This works perfectly but I don't understand the logic behind it. Can you recommend any videos or resources to understand why I need to do this for compiling. FYI I am very new to Java – Dev Feb 23 '23 at 16:45
  • @Dev I don't have a video. When you build a java class, javac needs to know about all of the required classes. You can do that in two ways, include the .class file on the classpath or include the source ( .java) to javac. When you run a program java needs to know all of the .class files. That's why when you did javac BankAccount.java BankTest.java it worked. When you tried to do javac BankTest.java alone, it couldn't find the BankAccount.class. – matt Feb 24 '23 at 07:46