AccountTest.java:10: 错误:找不到符号

huangapple go评论78阅读模式
英文:

AccountTest.java:10: error: cannot find symbol

问题

以下是翻译好的内容:

我目前正在学习Java,我尝试了教材中的一个示例,但无法使其运行起来。我一直在收到“找不到符号”的错误。

这是Account类中的代码:

package account;

public class Account {

    private String name;

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

    public String getName() {
        return name;
    }

}

而这是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("初始名称为:%s%n%n", myAccount.getName());

        System.out.println("请输入名称:");
        String theName = input.nextLine();
        myAccount.setName(theName);
        System.out.println();
        System.out.printf("myAccount 对象中的名称为:%n%s%n", myAccount.getName());
    }
}

我得到的错误消息是:

javac AccountTest.java
AccountTest.java:10: 错误: 找不到符号
            Account myAccount;
            ^
  符号:   类 Account
  位置: 类 AccountTest
AccountTest.java:11: 错误: 找不到符号
            myAccount = new Account();
                            ^
  符号:   类 Account
  位置: 类 AccountTest
2 个错误

为新手问题表示抱歉,感谢您的帮助!

英文:

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!

答案1

得分: 0

你的代码运行得很好。当你在集成开发环境(IDE)中使用时,它正常工作。但是当你像这样从控制台编译它时:

cd account  //<-- 移动到包含Account和AccountTest类的包中
javac AccountTest.java

你可能会遇到这个问题。问题的原因是javac不会自动包含所有导入的文件或当前目录中的文件(IDE会自动处理这些)。所以你需要告诉javac编译时要包括所有的文件:

javac AccountTest.java Account.java

编译完成后,你会在文件夹中看到AccountTest.classAccount.class文件。

最后,要从控制台运行你的代码,你应该执行:

java -classpath . AccountTest

附注: 可以参考这个相关的回答 https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows

英文:

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

cd account &lt;-- 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 https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows

答案2

得分: 0

我已经完成了这个,在我尝试运行javac Account.java时,它没有给我错误代码。

从命令行编译时,你需要同时指定两个类。

javac -cp . account/*.java

然后运行

java -cp . account.AccountTest

来执行它。

英文:

> 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.

huangapple
  • 本文由 发表于 2020年9月26日 14:54:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64074776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定