Java – 无法找到符号。两个文件在同一个目录中。

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

Java - cannot find symbol. Both Files in same directory

问题

BankTest.java:3: 错误: 找不到符号
BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
^
符号: 类 BankAccount
位置: 类 BankTest
BankTest.java:3: 错误: 找不到符号
BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
^
符号: 类 BankAccount
位置: 类 BankTest
BankTest.java:12: 错误: 找不到符号
BankAccount c = new BankAccount( args[0], args[1] );
^
符号: 类 BankAccount
位置: 类 BankTest
BankTest.java:12: 错误: 找不到符号
BankAccount c = a BankAccount( args[0], args[1] );
^
符号: 类 BankAccount
位置: 类 BankTest
BankTest.java:15: 错误: 找不到符号
BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
^
符号: 类 BankAccount
位置: 类 BankTest
BankTest.java:15: 错误: 找不到符号
BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
^
符号: 类 BankAccount
位置: 类 BankTest

我将上述代码的错误信息翻译成中文,但不包括其他内容。

英文:

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

        }

    }
}

答案1

得分: 1

你需要编译这些类,并将它们放在你的类路径上。一个好的管理方法是使用 -d 选项。确保目录 "build" 存在。

javac -d build BankAccount.java

这将编译并在 "build" 文件夹中创建一个 BankAccount.class 文件。然后你可以执行以下命令。

javac -cp build -d build BankTest.java

这将在 build 文件夹中创建一个 BankTest.class 文件。然后你可以运行它。

java -cp build BankTest

你需要将 .class 文件放在类路径上。这就是为什么 -d 选项很有效,因为你知道要将哪个文件夹放在类路径上,它还会为包创建目录,并将类文件放在正确的位置。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月16日 17:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470588.html
匿名

发表评论

匿名网友

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

确定