• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to create a package

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wondering if there's something more to creating a package than just putting the words "package" + name at the top of a file.
eg:
package mypackage;
import.....
thanks,
Annette
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not only add the package statement at the top of the file, but also create an appropriate directory structure so that packages match directories( and subpackages match subdirectories and so on )
I am moving this thread to Java in General(beginner) forum.
Ajith
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
there is definitely more than just a simple package statement.
first of all the packlage statement should be the topmost statenment of your source code.
whatever is the name of the package,u have to include your class file in the same directory.
e.g Package aa;
this has to be included in the directory aa.
any subpackages of the same have to be included within this folder as subfolders .
one more important aspect is that whenever u make a package and want to access the same,u have to set the classpath of the directory in which that package has been declared.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can specify the -d option to the javac compiler. This option tells the compiler where to put your output. It will also create the directory matching your package definition. Later you can use the -classpath option when you're invoking the JVM to the same directory and the JVM will find the correct class in the package hierarchy. Note, you have to specify the package with the class name when using this technique.
-Peter

If I compile this code with the command javac -d c:\work\lib Bar.java, the compiler will create the following directory structure for me (assuming it hasn't already existed). Additional note, the directory structure c:\work\lib should already exist. The compiler only creates your directory structure matching your package name.
<pre>
c:\work
|_lib
|_com
|_peter
|_foo
|_Bar.class
</pre>
Now to invoke the main() method of class Bar, I can drill down the directory structure:
java c:\work\lib\com\peter\foo\Bar
Or provide the package and class name and use the -classpath option to specify where to start looking for the root of my class.
java -classpath c:\work\lib com.peter.foo.Bar
Notice the package name with the class name. This is required when you're provide the root package using the classpath option.
-Peter

[This message has been edited by Peter Tran (edited January 15, 2001).]
[This message has been edited by Peter Tran (edited January 15, 2001).]
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic