英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论