Reputation: 33
package MyPack;
class Balance
{
String name;
protected double bal;
Balance(String n, double b)
{
name=n;
bal=b;
}
void show()
{
if(bal<0)
System.out.print("--> ");
System.out.println(name+": $" +bal);
}
}
class : AccountBalance
package MyPack;
class AccountBalance
{
public static void main(String[] args)
{
Balance current[]=new Balance[3];
current[0]=new Balance("K. J. Fielding", 123.23);
current[1]=new Balance("will Tell", 157.02);
current[2]=new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
I have put both these classes in Balance.java and AccountBalance.java . both files are in E:/programs/MyPack . Balance.java compiles without error But when I compile AccountBalance.java it gives error : cannot find symbol "Balance".
I'm unable to figure out why when both classes are declared in same package?
I'm compiling from MyPack using javac Balance.java javac AccountBalance.java
Upvotes: 3
Views: 3717
Reputation: 280168
Assuming you're issuing your javac
command from some folder other than E:/programs
, you'll need to specify a -cp
option including the location that includes your Balance
class.
This is because javac
uses the current directory if the option isn't specified
If neither
CLASSPATH
,-cp
nor-classpath
is specified, the user class path consists of the current directory.
So if you did, for example,
E:/> javac programs/MyPack/AccountBalance.java
then the Balance
class will not be in the classpath and the compiler will give you the error you see.
In that case, you'll need to specify an explicit location for your classpath. For example
E:/> javac -cp programs programs/MyPack/AccountBalance.java
Since Balance
is in package MyPack
which is at the root of /E/programs
, the compiler finds it and can use it.
Use an IDE.
Upvotes: 2
Reputation: 12041
Assuming you use javac
the reason is that you compile them one by one (first Balance
then AccountBalance
) and you are not in the parent folder of MyPack
. If you are really doing so, then please use the -cp
option of javac
to point where is the already compiled Balance.class
. For example:
..\so\src\MyPack>javac Balance.java
..\so\src\MyPack>javac -cp ../. AccountBalance.java
Alternatively compule them both, e.g:
..\so\src\MyPack>javac *.java
Upvotes: 0