Package is a first topic of java
Definition of Package:- package is the collection of similar type of classe
Interfaces and subpackages
Or
Java categorize there classes and interface and subpackages into a logic
-al container.
Categorization fast the search.
Why package is created in java?
Ans :fast the compiler to link and search
Provide unique name
Easy maintenance of the project
Source file will store in src.zip if u wanna see then go to C:\program files\java\jdk\src.zip
Compile file will store in rt.jar if u wanna see then go to C:\programfiles\java\jdk\jre\lib\rt.jar but after java 9 this postion is changed
Package is a nothing but a folder
In java every thing in method and method in class if you want to use the class then you can use by inheritance or association and we have to attach the package in our project otherwise you will get compile time error .
How to create a UserDefine Package?
We have to follow 2 steps to create User Define Package
1 -> creating logical package
i) Create logical package you must have write at the top of the class package com.coder.java // package name in reverse order of domain class Test{ } when we run this it will not run because we have 2 create a physical package as well to run this project so for this 2 step : we have 2 create the folder in the same directory with logical name of package . and put our Byte code inside it .com folder->coder folder-> java folder->put our byte code inside it and now run with this command : java com.coder.java.Test how to create a both logical and physical package in single stage package com.coder.java class Test{ }
compile : java –d . Test.java
directory -d is use to tell the compiler to create byte code in the folder name and suppose we are at c: desktop and (.) it is current working directory (you can also change it like e:\ or d:\) what happen with these line compiler check the project there is a logical name present in the project if it is then it crate a pakcgae in that cureent working directory .
Run : java com.coder.java.Test
Path vs classpath
Path is use to set the exe file location
Classpath is use to set file location
Both are environment variables when we have to tell the class file path then we use class path.
Lets assume we are at c:\desktop java –d d:\abc Test.java now class file is in d drive at we are at desktop if we run this from desktop location then it will not run for this to run we have to set class path.
Classpath=d:\abc;
Run: java com.coder.java.Test
Another method :
Lets assume we are at c:\desktop and we want to run from this location
java –classpath d:\abc Test.java or CP
Run: java com.coder.java.Test
If we don’t want to compile with class path C:\desktop\p here is my package and I want to create a jar of that package then C:\desktop\p jar cvf myjar p;cvf: create verbose file now your jar is created and you can run you class inside this package from anywhere.
And put your jar c:\programfile\java\jdk\jre\lib\ext and now run from any location Jar
file: desktop application
War :Web application
Ear :enterprise applcation
Whenever any class file compiles then compiler do to search file 1st of its goes to bootstrap class loader and this class is associated with rt after that extension loader which is associated with ext folder then application loader which is associated with classpath if class is not there then It search current directory if class is not there it will shows classnot found exception.
example 1 illustrate create logical and physical package at 1 line .in that current current directory
If we want to change the location of creation of package from current directory to somewhere else it means your class file is located in where You create your class file in beginning but you want to change the packaGe location from current folder and so that you can run from any where Without going to the location where package is then you will see Coder Image in document where you can see the commands.
When you compile the program from where you are creating your file
And you change the package location from this location to somewhere
Then you can do this thing to run
Classpath=; enter will remove the class path; when you set class path Then your class file in another location and you are at different location still you can run your program. This is all thing I ‘m telling for Command Prompt when you use IDE like eclipse ,NetBeans or something then you no need to do this things. And this path is temporary. Class path is alwaYs set for directory where your package resides.
How to create a Jar file of an Package?
package P2;
class B
{
public static void main(String args[])
P1.A a =new P1.A();
a.show();
System.out.println(a.x);
}
}
So there is 2method to use
1) another package class 1 you attach package in you class and inherit or create an object of that class.
2) if u don’t want to attach another package in your classB then class A all methods and varable constructor must be public
package P1;
class A
{
int x=10;
void show(){
System.out.println(" Hello A");
}
}
We first compile A class then its successfully compile but when we compile class B then we got following an errors because Class A is in different package and class B in another package
package P1;
public class A
{
Public int x=10;
Public void show(){
System.out.println(" Hello A");
}
}
2 method)
package P2;
import P1.A;
class B extends A
{
public static void main(String args[])
A a =new A();
a.show();
System.out.println(a.x);
} }
package P1;
public class A
{
Public int x=10;
Public void show(){
System.out.println(" Hello A");
}
}
In java before using a class we have 2 attach the package of a specific class which we are using.
How to attach package in our program?
1) By importing package
2) Without importing package(we have to provide fully qualified name)
And by default java.lang package is implicitly added into our program
This is the only package we don’t have to import
}
}
If you like Codersarts blog and looking for Programming Assignment Help Service,Database Development Service,Web development service,Mobile App Development, Project help, Hire Software Developer,Programming tutors help and suggestion you can send mail at contact@codersarts.com.
Please write your suggestion in comment section below if you find anything incorrect in this blog post
Comments