This project demonstrates a simple multi-file Java application where: - A.java is the main class that uses classes from B.java and C.java - B.java provides utility functions (mathematical operations) - C.java provides data processing functions (string and array operations)
Project Structure
abc_project/
├── A.java # Main class - uses B and C
├── B.java # Utility class with mathematical operations
├── C.java # Processor class with data processing operations
└── README.md # This file
How to Compile and Run
Method 1: Compile All Files Together (Recommended)
-
Navigate to the project directory:
bash cd projects/abc_project -
Compile all Java files:
bash javac *.javaThis will compile A.java, B.java, and C.java and create corresponding.classfiles (A.class, B.class, C.class). -
Run the program:
bash java A
Method 2: Compile Individual Files (In Order)
If you prefer to compile files individually, you need to compile in the correct order:
-
Navigate to the project directory:
bash cd projects/abc_project -
Compile B.java first (since A depends on B):
bash javac B.java -
Compile C.java (since A depends on C):
bash javac C.java -
Compile A.java (the main class):
bash javac A.java -
Run the program:
bash java A
Method 3: Compile with Output Directory
For better organization, you can compile to a separate directory:
-
Create a bin directory:
bash mkdir bin -
Compile all files to the bin directory:
bash javac -d bin *.java -
Run from the bin directory:
bash java -cp bin A
Or run from the project root:
bash
java -cp bin A
Expected Output
When you run the program, you should see output like:
=== Multi-File Java Project Demo ===
--- Using B class methods ---
Sum of 10 and 20: 30
Product of 5 and 6: 30
Is 15 even? false
Is 16 even? true
--- Using C class methods ---
Hello, Alice! Welcome to the program.
Array: [1, 2, 3, 4, 5]
Maximum value: 5
Minimum value: 1
Reversed 'Hello World': dlroW olleH
--- Combining B and C functionality ---
Array: [12, 45, 7, 23, 56]
Maximum value: 56
Is maximum value even? true
=== Project Structure ===
abc_project/
├── A.java (Main class - uses B and C)
├── B.java (Utility class)
└── C.java (Processor class)
Key Concepts Demonstrated
- Multiple Java Files: Working with multiple
.javafiles in the same project - Class Dependencies: A.java depends on B.java and C.java
- Compilation Order: Understanding that dependent classes need to be compiled
- Class Usage: Creating instances and calling methods from other classes
- No Packages: Simple example without package declarations (all classes in default package)
Troubleshooting
Error: "cannot find symbol"
- Solution: Make sure all files (A.java, B.java, C.java) are in the same directory
- Solution: Compile all files together using
javac *.java
Error: "class file not found"
- Solution: Make sure you've compiled all files before running
- Solution: Check that
.classfiles exist in the same directory
Error: "main method not found"
- Solution: Make sure you're running
java A(notjava A.javaorjava a)
Notes
- All classes are in the default package (no package declaration)
- All files must be in the same directory for this simple example
- The
javac *.javacommand compiles all.javafiles in the current directory - The
java Acommand runs the main method in class A