Packages and Classpath:
Lets see how to compile a Java program using packages from command line
First we created a folder 'Pac1' in D: drive.Inside 'Pac1' we created another folder called 'Pac2' and inside 'Pac2' we created another folder called 'Pac3'
Java Program : Write or copy the following code in notepad and save the file as A.java
A.javapackage pac2.pac3;
Class A
{
public static void main(String[] args)
{
System.out.println("Welcome to Packages");
}
}
Method-1: Executing Java program using packages through command line
Go to Command Prompt
For compiling Java Program:
D:\Pac1>javac Pac2\Pac3\A.java
So here we have to go to the root folder to compile the program
For running/executing the java program:
D:\Pac1>java Pac2.Pac3.A
Welcome to Packages //Output displayed
The above method will not work unless the class path has a '.' in the classpath of environment variables.See below how to set the classpath
Setting the CLASSPATH:
Definition: Classpath is that environment variables which Java thinks contains the root of the package hierarchy
i.e Java starts to search from all the paths that is present in classpath
Go to Start> Right Click My Computer>Properties>Advance System Settings>Click Environment variables>Edit the Classpath
Add the (.;) in the beginning of classpath
(.;) : The '.' (Dot) will tell Java to search for the Java program from the current folder
Method-2: Executing Java program using packages through command line
Instead of going through all the above process.There is a easier method to set the classpath in environment variables
Go to Start> Right Click My Computer>Properties>Advance System Settings>Click Environment variables>Edit the Classpath
Add 'D:\Pac1;' in the beginning of classpath
This is telling Java to start search from 'D:\Pac1' folder, even if you somewhere in E or F or other drive or folder
This will allow you to run the Java program from any drive or folder from the command line
Click for private tuition by a professional
Lets see how to compile a Java program using packages from command line
First we created a folder 'Pac1' in D: drive.Inside 'Pac1' we created another folder called 'Pac2' and inside 'Pac2' we created another folder called 'Pac3'
Java Program : Write or copy the following code in notepad and save the file as A.java
A.javapackage pac2.pac3;
Class A
{
public static void main(String[] args)
{
System.out.println("Welcome to Packages");
}
}
Method-1: Executing Java program using packages through command line
Go to Command Prompt
For compiling Java Program:
D:\Pac1>javac Pac2\Pac3\A.java
So here we have to go to the root folder to compile the program
For running/executing the java program:
D:\Pac1>java Pac2.Pac3.A
Welcome to Packages //Output displayed
The above method will not work unless the class path has a '.' in the classpath of environment variables.See below how to set the classpath
Setting the CLASSPATH:
Definition: Classpath is that environment variables which Java thinks contains the root of the package hierarchy
i.e Java starts to search from all the paths that is present in classpath
Go to Start> Right Click My Computer>Properties>Advance System Settings>Click Environment variables>Edit the Classpath
Add the (.;) in the beginning of classpath
(.;) : The '.' (Dot) will tell Java to search for the Java program from the current folder
Method-2: Executing Java program using packages through command line
Instead of going through all the above process.There is a easier method to set the classpath in environment variables
Go to Start> Right Click My Computer>Properties>Advance System Settings>Click Environment variables>Edit the Classpath
Add 'D:\Pac1;' in the beginning of classpath
This is telling Java to start search from 'D:\Pac1' folder, even if you somewhere in E or F or other drive or folder
This will allow you to run the Java program from any drive or folder from the command line
Click for private tuition by a professional