faqts : Computers : Programming : Languages : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 11 people (73%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Java: Package: How to create a package? [import]

May 25th, 2006 19:47
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 26 May 2021 - 01:01 am ------------------------
Java: Package: How to create a package? [import]
===
On top of your Java .java program, put the word
 package
followed by the path (directories separated by points)
Then compile it using javac.exe with the -d option.
This will create one directory (and zero or more subdirectories)
(e.g. the directory where your .java program is located,
      e.g. the current directory)
where your compiled .java class file is located.
===
Steps: Overview:
 1. -Put the word 'package <your package name>'
     text on top of your java program file
     1. -E.g.
          package yourPackage;
          ...
     1. -E.g.
          package yourPackage.yourSubDirectory1.yourSubDirectory11;
          ...
 2. -Compile your program with the '-d' option
     (here the '.' means the current directory)
     javac.exe -d . <your java filename>
    1. -E.g.
         javac.exe -d . HelloWorld.java
    1. -E.g.
         javac.exe -d . *
 3. -This will create a directory called
     (e.g. in the current directory)
      <your package name>
     1. -E.g. a directory for your package, called
          yourPackage
 4. -Run your compiled program via
      java.exe <your package name>.<your program name>
     1. -E.g.
          java yourPackage.HelloWorld
 5. -To use that package (e.g. that class) in another program, use
     the keyword 'import'
     1. -E.g.
          import <your packagename>;
          ...
         1. -E.g.
              import yourPackage;
===
Steps: Worked out:
 1. -Put the word 'package <your package name>'
     text on top of your java program file
     1. -E.g.
--- cut here: begin --------------------------------------------------
package yourPackage;
class HelloWorldApplication {
 //
 public static void main( String args[] ) {
  System.out.println( "Hello World" );
  }
 //
}
--- cut here: end ----------------------------------------------------
 2. -Compile your program with the '-d' option
     (here the '.' means the current directory)
     javac.exe -d . HelloWorldApplication.java
 3. -This will create a directory called
     (e.g. here in the current directory)
--- cut here: begin --------------------------------------------------
> dir
 Directory of  C:\*
 5/26/2006   0:51         <DIR>    .
 5/26/2006   0:51         <DIR>    ..
 5/26/2006   0:51         <DIR>    yourPackage
> dir yourPackage
 Directory of  C:\yourPackage\*
 5/26/2006   0:36         <DIR>    .
 5/26/2006   0:36         <DIR>    ..
 5/26/2006   0:36             459  HelloWorldApplication.class
--- cut here: end ----------------------------------------------------
 4. -Run your compiled program via
      java.exe <your package name>.<your program name>
     1. -E.g.
          java.exe -classpath . yourPackage.HelloWorldApplication
         1. -That will show a screen output similar to the following:
--- cut here: begin --------------------------------------------------
> java.exe -classpath . yourPackage.HelloWorldApplication
  Hello World
--- cut here: end ----------------------------------------------------
 5. -To use that package (e.g. the class 'HelloWorldApplication') in
     another program, use 'import'
      import yourPackage;
===
Steps: Worked out:
 1. -Put the word 'package <your package name>'
     text on top of your java program file
     (in this case a directory with subdirectories)
     1. -E.g.
--- cut here: begin --------------------------------------------------
package yourPackage.yourSubDirectory1.yourSubDirectory11;
class HelloWorldApplication {
 //
 public static void main( String args[] ) {
  System.out.println( "Hello World" );
  }
 //
}
--- cut here: end ----------------------------------------------------
 2. -Compile your program with the '-d' option
     (here the '.' means the current directory)
     javac.exe -d . HelloWorldApplication.java
 3. -This will create a directory called
     (e.g. here in the current directory)
--- cut here: begin --------------------------------------------------
> dir
 Directory of  C:\*
 5/26/2006   0:51         <DIR>    .
 5/26/2006   0:51         <DIR>    ..
 5/26/2006   0:51         <DIR>    yourPackage
> dir yourPackage
 5/26/2006   1:03         <DIR>    .
 5/26/2006   1:03         <DIR>    ..
 5/26/2006   1:03         <DIR>    yourSubDirectory1
> dir yourPackage\yourSubDirectory1\
 5/26/2006   1:03         <DIR>    .
 5/26/2006   1:03         <DIR>    ..
 5/26/2006   1:03         <DIR>    yourSubDirectory11
> dir yourPackage\yourSubDirectory1\yourSubDirectory11\
 5/26/2006   1:03         <DIR>    .
 5/26/2006   1:03         <DIR>    ..
 5/26/2006   1:03             496  HelloWorldApplication.class
--- cut here: end ----------------------------------------------------
 4. -Run your compiled program via
      java.exe <your package name>.<your program name>
     1. -E.g.
          java.exe -classpath . yourPackage.HelloWorldApplication
         1. -That will show a screen output similar to the following:
--- cut here: begin --------------------------------------------------
> java.exe -classpath . 
yourPackage.yourSubDirectory1.yourSubDirectory11.HelloWorldApplication
   Hello World
--- cut here: end ----------------------------------------------------
 5. -To use that package (e.g. the class 'HelloWorldApplication') in
     another program, use 'import'
      import yourPackage.yourSubDirectory1.yourSubDirectory11;
===
Tested successfully on
Microsoft Windows XP Professional (service pack 2)
running java SDK 1.4.2.01
===
Internet: see also:
---
----------------------------------------------------------------------