Java Programming Drashti Baldev
Simplified Package Program
Uptil now we used to save source files(.java) and class files(.class) in same folder that was
D:\my folder.
Programs were written without package and import statements. So to compile and run program
in command prompt we use to go till the folder where my source file was stored that was till
D:\my.
Java Programming Drashti Baldev
Steps
1) First create manually the folders in which you want to store source and
class files.
Here
Source files will be stored in D:\package\src folder.
Class files will be stored in D:\package\class folder
Java Programming Drashti Baldev
2) Create source file Student.java & save it in D:\package\src.
1. package statement should be first line of
code.
2. It indicates that Student.class file will be
stored in folder named college->computer
3) Compile Student.java source file.
In order to compile any source file using command prompt, go to folder where source files are
stored.
Java Programming Drashti Baldev
This stores class file in the same
folder from where it is compiled
that is D:\pacakge\src
“Dont do this”
javac <options> <sourcefile name>
-d is the option which means specify where
to place generated class files.
Here I want place class file in
D:\package\class folder.
javac -d [path_to_store_classfile] [sourcefilename]
javac -d . [sourcefilename] => here . (dot) means current folder.
Java Programming Drashti Baldev
Java Programming Drashti Baldev
4) Create source file StudentDemo.java & save it in D:\package\src.
D:
package
src class
Student.java college
StudentDemo.java computer
Student.class
OR
Java Programming Drashti Baldev
5) Compile StudentDemo.java source file.
We are compiling from D:\package\src
This file it has import statement as import college.computer.Student;
Whenever javac compiler tries to compile import statement it will search for Student.class file.
Two searches
1) same directory------------------------------- not found
2) It will refer classpath variable
classpath variable set in Command Prompt (temporary)
classpath variable set as environment variable (permanent)
Java Programming Drashti Baldev
6) Run StudentDemo.class class file.
Two ways to run this file
1) from anywhere if classpath is already set.
2) if no package statement in corresponding source file ---- then from folder it is stored.
if package statement in corresponding source file ------then above folder of the package stmt.
OR
Java Programming Drashti Baldev
Now consider if both the files have package statement
Source files will be stored in D:\package\src folder.
Class files will be stored in D:\package\class folder
Java Programming Drashti Baldev
Or
Java Programming Drashti Baldev
OR
Java Programming Drashti Baldev
Example 2
A.java (D:\package\src)
package com.general;
public class A
{
public void display()
{
System.out.println("Java Programming");
}
}
B.java (D:\package\src)
import com.general.*;
public class B
{
public static void main(String args[])
{
A a = new A();
a.display();
}
}
Java Programming Drashti Baldev
Summarize
Source files will be stored in D:\package\src folder.
Class files will be stored in D:\package\class folder
package com.general;
public class A
{
public void display()
{
System.out.println("Java Programming");
}
}
package com.classfiles;
import com.general.A;
public class B
{
public static void main(String args[])
{
A a = new A();
a.display();
}
}