File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes packages::::::: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "packages:::::::" Watch "packages:::::::" New topic



Joined: Feb 26, 2001
Posts: 6
I am learning a new topic called packages. interesting one but there is a problem, I am unable to understand untill now how we compile a package which have two or more classes and how to run a package.
So, if some one answer me I am very thankful to him. I know packages but the problem is for command to compile and run the packages.

Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
You cannot compile or run a package in java.
Package is something used to avoid name collisions. Its like a namespace and has an hierarchy.
for example....
in my .jar file there are two classes
Person.class - under com/prasad
Person.class - under com/prasad/Misc
if this jar file is in the classpath and I try to compile the following code I get compiler errors
Person = new Person();
because the compiler doesnot know which Person to refer.
to make it clear to the compiler I refer them I can refer like this....
com.prasad.Person n = new com.prasad.Person();
com.prasad.Misc.Person n = new com.prasad.Misc.Person();
another way to do this is.. importing the specific package
import com.prasad.*;
Person = new Person();
import com.prasad.Misc.*;
Person = new Person();
but you cannot import both packages when you create a new Person Object
import com.prasad.*;
import com.prasad.Misc.*;
Person = new Person();
The compiler complains about the ambiguity.
Now how to create a package?
Just write this statemet at the begining of your .java file
package <packagename>
package com.prasad;
public class Person
package com.prasad.Misc;
public class Person extends xxx
Hope this helps
NageswaraRao Karra
Ranch Hand

Joined: Nov 12, 2000
Posts: 34
Hello Adnan!
for compiling yr. java files with package notation intact use the following.
c:>javac -d
then it creates a directory structure with yr.package name as parent to the class file.
for execution of such class files
use the following
java package.anyclassfile
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
By the way, Java life is easier if you remember this rule: ALL class names must ALWAYS be qualified with their package. (By default java imports "java.lang,*", which make this rule less obvious.) For example, to reference class A in package mypackage you must code "mypackage.A" or use an import statement. If "A" has a main method then you run it by passing "mypackage.A" to java. This assumes your classpath is set. I.e., that your classpath includes the root location where directory "mypackage" is found.
I agree. Here's the link:
subject: packages:::::::
It's not a secret anymore!