my dog learned polymorphism
The moose likes Beginning Java and the fly likes Package Problem 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 » Beginning Java
Bookmark "Package Problem" Watch "Package Problem" New topic

Package Problem

Shardul Kumar

Joined: May 24, 2009
Posts: 12
What does it exactly mean to have a package statement in the beginning of a java code.
Does it mean :
1> The class file generated after compiling the java file should be in a directory having same name as in the package statement?

PS : Am I missing some more concepts?

Please Help!!!
prakash pawar
Ranch Hand

Joined: Mar 11, 2010
Posts: 42
Hi ,

Prakash Pawar.
Alex Parvan
Ranch Hand

Joined: Dec 10, 2009
Posts: 115
Consider the "src" folder the default package. Every folder you create inside "src" is considered a package:

If you have a .java file in "client", it will start with the line:
package pack2.client;

It's very simple. Hope this helps.

"Quoting yourself is stupid" - Me
Shardul Kumar

Joined: May 24, 2009
Posts: 12
Yeah Thnx!!
It helped a lot
Thank you guys
Rahul Kurup

Joined: Dec 11, 2009
Posts: 9
Packages are a concept to manage huge projects. Using this related classes are grouped together.

Let us say this is the folder structure you have created for saving your .java files. Also lets say your program( has the statement

Now lets say at command prompt we are in the folder src. If you execute,

javac pack2/client/

we are saying using the source file pack2/client/ of what is mentioned in the command being executed) store the class file to the location pack2/client(because of the package statement) under the current location(src) AND if the folder structure does not already exist(which is not the case here) create it and then store.So here the class files will be created in src/pack/client itself.

Lets say your source file is not at src/pack/client. It is in src itself and at command prompt we move to src location.

Now if we execute this.


we are saying that from current folder the class files have to be created in pack2/client(through the package statement). But this time pack and client folder will be automatically created and then class files will be stored there.

Now if you want the following structure:

i.e. source files at one place and class files at another place

Navigate to src location from command prompt and execute

javac -d ../classes pack2/client/

Your source files are already in src/pack2/client and class files will be created in classes/pack2/client.

So according to me package declaration is a way of saying in which folder structure should the class files be stored irrespective of where your source file is.

Hope I have not confused you. I am still not that much into IDEs.

Rahul J.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46425
Shardul Kumar wrote:Yeah Thnx!!
You have been here long enough to know not to write things like thnx.
I agree. Here's the link:
subject: Package Problem
It's not a secret anymore!