How to Create Java Packages in Eclipse IDE



This tutorial will help you to create a Java package in Eclipse IDE Kepler. The package describes a namespace in which classes are stored. If omit a package from java file, it will be a default package. You can make basic programs with default packages. If you want to create a complex application, you should consider Java's package.
java package example eclipse

Let's see how to create a Java package in Eclipse 


Step 1: Open up the Eclipse IDE
Step 2: Create a new project File-> New -> Java Project name it as “SimplePackage” .
Step 3: Create a new class called Company

Create a package client.data.book then drag and drop Company class to newly created package client.data.book. It will clear default package from your project explorer of Eclipse IDE.
Now client.data.book package has got a class Company Add the following code to Company class.
Company.java

package client.data.book;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import server.data.book.Server;
public class Company {

 public static void main(String args[]) throws IOException
 {
 BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); 
 System.out.println("Enter URL:");
 String h=b.readLine();
 System.out.println("Enter PORT:");
 int p=Integer.parseInt(b.readLine());
 Server s=new Server(h,p);
 s.setHost();
 s.viewStatus(s);
 }

}
Next you have to create server.data.book package, it includes a class called Server which is already instantiated in the Company Class.

Server.java


package server.data.book;
public class Server {
 String URL;
 int port;
public Server(String URL,int port)
{
 this.URL=URL;
 this.port=port;
}
public void setHost()
{
System.out.println("Host Name :"+URL);

System.out.println("Port Number:"+String.valueOf(port));

}
public void viewStatus(Server s)
{

 System.out.println(s.URL+"is Working at the port of"+String.valueOf(s.port));
}
}
You might like :

JSP MySQL Eclipse JEE Tutorial 

How to Create Java Packages in Eclipse IDE How to Create Java Packages in Eclipse IDE Reviewed by Nikin on 09 March Rating: 5
Powered by Blogger.