How to compile and execute java package program?

UPDATED: 15 October 2010

What is Package?
Phenomenon behind creating package is very good for developer. In a simple language we can say that "Package" helps us to categorize our code in different folders. Lets say if you are creating some application and it has two different logic for code.

  • Validate inputs of program
  • Database transaction

Place validation code in package called "myApp.validate" and the code that work around database transaction put it in "myApp.Database". It makes easy to find your program when you want to modify. All major companies follow the package staructure that helps all developer to work faster.

  • package package_name must be first line of program as written in below code.
    eg. package com.javaquery.examples;

package com.javaquery.examples;

public class HelloWorld{
    public static void main(String args[]){
       System.out.println("Hello World!");

Compile and Run

  • Compile using : javac -d . While compiling . dot followed by -d is required.
  • Execute using : java com.javaquery.examples.Helloworld>
Note: Here -d argument generates directory as per the package declared