Skip to main content

Exploring Compilation

Syntax

javac -d <Location> <SourceFileName>.java

Example

javac -d . Lab.java

Important Notes

  • Here . (dot) is used as Location. → Package will be created in the current working directory and compiled classes will be placed inside that package.
  • If you want to create the package in a specific directory instead of current directory, then you must specify the location explicitly.
  • Assume the current working directory is:
D:\JavaWorld\D1\pack

🔥 Quick Understanding

  • javac -d → creates package folders automatically
  • . means current directory
  • Package structure mirrors folder structure
  • .class files go inside package folders

Hello.java

package com.JavaWorldIndia;

class Hello {

public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}

Case 1: Creating Package in CURRENT DIRECTORY

Relative Location

javac -d . Hello.java

Absolute Location

javac -d D:\JavaWorld\D1\pack Hello.java

Directory Structure (Case 1)

graph TD
D["D:\"] --> JavaWorld["JavaWorld"]
JavaWorld --> D1["D1"]
D1 --> PACK["PACK"]

PACK --> SRC["Hello.java"]

PACK --> com["com"]
com --> JavaWorldIndia["JavaWorldIndia"]
JavaWorldIndia --> CLASS["Hello.class"]

Case 2: Creating Package in OTHER DIRECTORY IN SAME DRIVE

Relative Location

javac -d class es Hello.java

Absolute Location

javac -d D:\JavaWorld\D1\pack\class es Hello.java

Directory Structure (Case 2)

Important Notes

  • javac -d creates the package directory structure automatically.
  • d . → creates package folders in the current directory.
  • d classes → creates package folders inside the classes directory.
  • Relative path → relative to current working directory.
  • Absolute path → full folder path.

🔥 Quick Understanding

Example:

If package is:

package com.JavaWorldIndia;

Compiler creates:

com/JavaWorldIndia/

and places:

Hello.class

inside it.

Compilation Flow