Java                                                          Home : www.sharetechnote.com

 

 

 

 

Java - Package  

 

 

 

Creating , Import and Run

 

 

In this example, I have created four java files in c:\temp folder as shown below. For now, there are only java files in this folder and there is no sub folders in it. Pay attention to how the folder structure changes as we compile these java files.

 

    c:\temp>dir

     

     Volume in drive C has no label.

     Volume Serial Number is 568E-ECD3

     

     Directory of c:\temp

     

    01/06/2018  01:39 AM    <DIR>          .

    01/06/2018  01:39 AM    <DIR>          ..

    01/05/2018  10:53 PM               144 Hello.java

    01/06/2018  01:33 AM               161 HelloWorld.java

    01/06/2018  01:35 AM               389 PrintMessage.java

    01/05/2018  10:53 PM               144 World.java

 

 

Contents of these files are as shown below.

 

HelloWorld.java

package MessageCollections.HelloWorld;

 

public class HelloWorld {

 

    public void ShowMessage() {

        System.out.println("Hello, World");

    }

 

}

 

 

Hello.java

package MessageCollections.Hello;

 

public class Hello {

 

    public void ShowMessage() {

        System.out.println("Hello");

    }

 

}

 

 

World.java

package MessageCollections.World;

 

public class World {

 

    public void ShowMessage() {

        System.out.println("World");

    }

 

}

 

 

PrintMessage.java

package MessageCollections;

 

import MessageCollections.HelloWorld.*;

import MessageCollections.Hello.*;

import MessageCollections.World.*;

 

public class PrintMessage {

 

    public static void main(String[] args) {

        HelloWorld hw = new HelloWorld();

        Hello h = new Hello();

        World w = new World();

        hw.ShowMessage();

        h.ShowMessage();

        w.ShowMessage();

    }

 

}

 

 

Compile each of the files using -d option as shown below. With '-d .' , all the package folders are created as sub folders in current folder.

    c:\temp>javac -d . HelloWorld.java

    c:\temp>javac -d . Hello.java

    c:\temp>javac -d . World.java

    c:\temp>javac -d . PrintMessage.java

 

 

Now list all the files including the subfolder. You would notice that several server folders are created by compile process as commented below.

 

    c:\temp>dir/s

     

     Volume in drive C has no label.

     Volume Serial Number is 568E-ECD3

     

    Directory of c:\temp

     

    01/05/2018  10:53 PM               144 Hello.java

    01/06/2018  01:33 AM               161 HelloWorld.java

    01/06/2018  02:45 AM    <DIR>          MessageCollections

    01/06/2018  01:35 AM               389 PrintMessage.java

    01/05/2018  10:53 PM               144 World.java

     

     

    Directory of c:\temp\MessageCollections <-- created by package MessageCollections;

     

    01/06/2018  02:45 AM    <DIR>          .

    01/06/2018  02:45 AM    <DIR>          ..

    01/06/2018  02:45 AM    <DIR>          Hello

    01/06/2018  02:43 AM    <DIR>          HelloWorld

    01/06/2018  02:45 AM               515 PrintMessage.class

    01/06/2018  02:45 AM    <DIR>          World

                   1 File(s)            515 bytes

     

     

    Directory of c:\temp\MessageCollections\Hello <-- created by package MessageCollections.Hello;

     

    01/06/2018  02:45 AM               416 Hello.class

     

     

    Directory of c:\temp\MessageCollections\HelloWorld

                                     <-- created by package MessageCollections.HelloWorld;

     

    01/06/2018  02:43 AM               438 HelloWorld.class

                   

     

     

    Directory of c:\temp\MessageCollections\World <-- created by package MessageCollections.World;

     

    01/06/2018  02:45 AM               416 World.class

                   

     

 

Now let's run a class. You can run the class with main() function as shown below. Note that I am running the class in the parent folder to the package folder.

 

    c:\temp>java MessageCollections.PrintMessage

     

    Hello, World

    Hello

    World

 

Just for your reference, I will show the result of running the program in different folder.

 

    c:\temp\MessageCollections>java PrintMessage

     

    Error: Could not find or load main class PrintMessage

     

     

     

    c:\temp\MessageCollections>java MessageCollections.PrintMessage

     

    Error: Could not find or load main class MessageCollections.PrintMessage