• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to use package?  RSS feed

 
jimmy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, guys, I got a problem in trying to take advantage of the java package structure. like this:
//------------
package a.b;
class One {
public int get(){return 8;}
}
//-----class Two try to use One
package a.c;
class Two{
public static void main(String ar[]){
int i = new a.b.One().get();
System.out.println(i);
}
}

and the directary is like : current/src (One.java and Two.java in this directary).
also exist current/classes

compile by useing the command like :
current : javac -d classes src/One.java
(works well and I get current/classes/a/b/One.class.)
current : javac -d classes src/Two.java
----------compile error, a.b.One package doesn't exist! ???
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to store the class files in a directory structure that matches your package structure. Also, you need to import a package if you want to use it.
 
jimmy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I got it.

The CLASSPATH environment variable must be set to point to any directories that contain Java classes that you want to import. To point to specific Java Packages, you must set the CLASSPATH to point to the directory ABOVE the package:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!