0

I'm currently learning java and i was trying out an example from my textbook but i can't get it to work. I keep getting the "cannot find symbol" error. This is the code in class Account:

package account;

public class Account {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

And this is for AccountTest:

package account;

import java.util.Scanner;

public class AccountTest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Account myAccount;
        myAccount = new Account();
        System.out.printf("initial name is %s%n%n", myAccount.getName());

        System.out.println("Please enter the name:");
        String theName = input.nextLine();
        myAccount.setName(theName);
        System.out.println();
        System.out.printf("name in object myAccount is: %n%s%n", myAccount.getName());
    }
}

The error message i get is:

javac AccountTest.java
AccountTest.java:10: error: cannot find symbol
            Account myAccount;
            ^
  symbol:   class Account
  location: class AccountTest
AccountTest.java:11: error: cannot find symbol
            myAccount = new Account();
                            ^
  symbol:   class Account
  location: class AccountTest
2 errors

Sorry for the newbie question and thanks for the help!

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35

2 Answers2

0

Your code work fine. When you use IDE - it work normally. But when you complile it like this from console:

cd account <-- move to your package with Account and AccountTest classes
javac AccountTest.java

You about to reseive this problem. Reason is javac does not include all imported files or files in the current directory automatically (this IDE does). So all you have to do is to tell javac all files for compile.

javac AccountTest.java Account.java

As result you will see AccountTest.class and Account.class files in your folder.

And finally to run your code from console you should do:

java -classpath . AccountTest

P.S. Look at this relevant answer How do I run a Java program from the command line on Windows?

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

I have already done that, when i tried to do javac Account.java it didn't give me an error code.

You need to specify both classes when you compile from the command line.

javac -cp . account/*.java

And then

java -cp . account.AccountTest

to run it.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • This is what i get: C:\Users\Daan\Documents\School\NetBeansProjects\Account\src\account>javac -cp . *.java C:\Users\Daan\Documents\School\NetBeansProjects\Account\src\account>java -cp . Account.AccountTest Error: Could not find or load main class Account.AccountTest – Daan Van De Velde Sep 26 '20 at 09:01
  • move up one folder to `src`. The class name includes the package name, and that maps to the folder structure. The [`javac` documentation](https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html) covers this. – Elliott Frisch Sep 26 '20 at 09:04