0% found this document useful (0 votes)
18 views13 pages

Java Packaging and Compilation Guide

1) Packages allow organizing Java code into folders and using import statements. 2) Source files (.java) are stored in the src folder under the package folder, and class files (.class) are stored in the class folder. 3) To compile source files with packages, the javac compiler searches for imported classes in the classpath, which includes the class folder.

Uploaded by

Hardvi Thoria
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

Java Packaging and Compilation Guide

1) Packages allow organizing Java code into folders and using import statements. 2) Source files (.java) are stored in the src folder under the package folder, and class files (.class) are stored in the class folder. 3) To compile source files with packages, the javac compiler searches for imported classes in the classpath, which includes the class folder.

Uploaded by

Hardvi Thoria
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link] & save it in D:\package\src.

1. package statement should be first line of


code.
2. It indicates that [Link] file will be
stored in folder named college->computer

3) Compile [Link] 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 [Link] & save it in D:\package\src.

D:

package

src class

[Link] college

[Link] computer

[Link]

OR
Java Programming Drashti Baldev

5) Compile [Link] source file.


We are compiling from D:\package\src

This file it has import statement as import [Link];

Whenever javac compiler tries to compile import statement it will search for [Link] 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 [Link] 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

[Link] (D:\package\src)

package [Link];
public class A
{
public void display()
{
[Link]("Java Programming");
}
}
[Link] (D:\package\src)

import [Link].*;
public class B
{
public static void main(String args[])
{
A a = new A();
[Link]();
}
}
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 [Link];
public class A
{
public void display()
{
[Link]("Java Programming");
}
}

package [Link];
import [Link].A;
public class B
{
public static void main(String args[])
{
A a = new A();
[Link]();
}
}

You might also like