Reputation: 661
Consider a package hierarchy folder1/hi. folder1 contains B.java and hi contains A.java.
B.java:
package hi.a12.pkg;
public class B { }
A.java:
package a12.pkg;
public class A {B b; }
Now B.java compiles successfully, but A.java does not.
Since both should produce class files in same location. Hence they should be able to find each other without import statement.
But still It says class B not found.
Anyone suggest the measures...or whats going wrong..
Upvotes: 3
Views: 1414
Reputation: 15212
Consider a package hierarchy folder1/hi. folder1 contains B.java and hi contains A.java.
So B.java
is in folder1
and A.java
is in a folder named hi
. So far so good.
B.java
looks like this :
package hi.a12.pkg;
public class B { }
Oops. B.java
says that it is in a package
named hi.a12.pkg
and yet it's physical location on the disk is folder1
. That's where the problem is. Put your files in the folder indicated by the package
statement or else other classes will not be able to find them.
A quick way to understand the concept and fix your problem would be to :
package
statement in B.java
to package folder1;
.package
statement in A.java
to package folder1.hi;
import
B
in A.java
after the package
statement as import folder.B;
B.java
from one directory above folder1
as javac folder1\B.java
A.java
from one directory above folder1
as javac folder1\hi\A.java
You can read all about it in the Oracle documentation
Upvotes: 2
Reputation: 103
There are several problems.
First, it looks like your packages are named the wrong way around. Try
for A.java (which should be in the directory ..../pkg/a12):
package pkg.a12;
for B.java (which must be in the directory .../pkg/a12/hi):
package pkg.a12.hi;
Second, your file A.java needs to say where B is located using an import statement:
package pkg.a12;
import pkg.a12.hi.B;
public class A {B b; }
Third, when you compile A you must be in the folder above pkg and refer to the full path of A:
javac pkg/a12/A.java
This will also compile B.java
Upvotes: -1