how to import user defined packages in java program? [closed]
some.java
import A.A1.*;
Directory structure
A
|_A2
| |_some.java
|_A1
|_someother files and java files
but import statement didn't work. What is the reason?
I hope A
is not your current directory.
Perhaps you already know that packages work with directory heirarchies.
<curr-dir>
|-A
| |-A1
| | |- Class1.class
| | `- Class2.class
| `-A2
| |-Class3.class
| `-Class4.class
`-<other dirs>
Now if <curr-dir>
is in your classpath then import A.A1.*
will import Class1 and Class2.
Hope that is detailed enough :)
Any class in a given package must have a package statement, and must be declared public (in most cases) to be read outside the package:
package A.A1;
public class ClassName{}
package A.A1;
import A.A2.*;
public class ex2{}
Dude *(star operator) does not work for user created packages! you need to import each class individually..
精彩评论