How to run Java package program

JavaJava Programming Java 8

Let us look at an example that creates a package called animals. It is a good practice to use names of packages with lower case letters to avoid any conflicts with the names of classes and interfaces.

Following package example contains interface named animals −

/* File name : */
package animals;
interface Animal {
   public void eat();
   public void travel();

Now, let us implement the above interface in the same package animals −

package animals;
/* File name : */
public class MammalInt implements Animal {
   public void eat() {
      System.out.println("Mammal eats");
   public void travel() {
      System.out.println("Mammal travels");
   public int noOfLegs() {
      return 0;
   public static void main(String args[]) {
      MammalInt m = new MammalInt();;;

Now compile the java files as shown below −

$ javac -d .
$ javac -d .

Now a package/folder with the name animals will be created in the current directory and these class files will be placed in it as shown below.

Create Directory

You can execute the class file within the package

$ java animals.MammalInt

and get the result as shown below.

Mammal eats
Mammal travels
Published on 05-Feb-2018 15:34:40