3
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

Abhi Abhinit
  • 33
  • 1
  • 5

2 Answers2

2

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • If you are in `E:/programs/MyPack` you should also specify cp... `E:/programs` is OK. – Lachezar Balev Jan 22 '15 at 20:20
  • You have to compile Balance.java from E:/programs/MyPack using : javac Balance. java now you compile from E:/programs/MyPack using : javac -cp E:/programs AccountBalance.java or, javac -classpath E:/programs AccountBalance.java if you compile from E:/programs then use javac E:/programs/MyPack/AccountBalance.java nothing else works.. – Abhi Abhinit Jan 22 '15 at 21:09
  • @abhi It doesn't matter whete you compile it from. What matters is that the classpath is correct and contains the required classes. – Sotirios Delimanolis Jan 22 '15 at 21:50
0

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
Lachezar Balev
  • 11,498
  • 9
  • 49
  • 72
  • No it isn't. If the compiler is used correctly it can compile them one by one in either order. He could also use the `-sourcepath` option, or best of all he could just put himself into the correct working directory before issuing the command, that is to say the directory containing the `MyPack` directory. – user207421 Jan 22 '15 at 20:12