aspose file tools*
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 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package Problem" Watch "Package Problem" New topic
Author

Package Problem

Shardul Kumar
Greenhorn

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 ,

http://java.sun.com/docs/codeconv/html/CodeConventions.doc2.html


Regards,
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
Greenhorn

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

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(YourProgram.java) has the statement

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

javac pack2/client/YourProgram.java

we are saying using the source file pack2/client/YourProgram.java(because 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.

javac YourProgram.java

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/YourProgram.java

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
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Shardul Kumar wrote:Yeah Thnx!!
You have been here long enough to know not to write things like thnx.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Package Problem
 
Similar Threads
Compile with the package statement
Classpath issue?
Apply your Lateral Thinking
a top level class in a package?
Enable assertions in current package.