ALGORITHM _> HOW JAVA COMPILER SEARCHES FOR CLASS FILE
Hola folks, Welcome again !!!!!,
I know it’s been so long but i am back again with very mind blowing and interesting blog on java compiler. This blog will give you an idea about java compiler workflow.
We will be seeing how java compiler searches for class definition…
First Create Two Java Files for testing [ Main.java, Sample.java]
SOURCE CODE
GitHub-link = https://github.com/tamimdalwai/JAVA-BLOG.git
Compile the Main.java and Sample.java File as a result two packages are generated “PackageMain” and “PackageSample”.
CASE 1 : Searches class Sample{ } in current method as LOCAL INNER CLASS
Now let’s execute the Main class
We can see , compiler first checked inside local main method for Sample class.
CASE 2: Searches class Sample{ } in Main.class as CLASS LEVEL INNER CLASS
Now let’s comment the Method Level Inner Class Sample and now compiler will search one step above i.e. in Class Level Inner Class in the same Main.class file.
Before working further delete the generated packages and again compile the “Main.java” file again with the help of javac command.
Run ->> java PackageMain.Main
CASE 3 : Searches class Sample { } in current java file (Main.java) as OUTER CLASS.
Now again do the same comment the Class Level Inner Class.
Before working further delete the generated packages and again compile the “Main.java” file again with the help of javac command.
Run ->> java PackageMain.Main
CASE 4 : Searches for class Sample{ } in CURRENT PACKAGE
Before working further delete the generated packages. Now manually create package “PackageMain” and create one Sample.java file inside that folder. Code for Sample.java
Package PackageMain;
public class Sample{
void method(){
System.out.println("Method executed from Sample.java file and PackageMain package i.e. current package");
}
}
Run ->> java PackageMain.Main
CASE 5 : Searches for class Sample{ } in IMPORTED PACKAGE
Delete the PackageMain folder and now if we try to compile Main.java file we get below error (cannot find symbol)
So basically compiler is unable to find class, we have to explicitly set the class path as below:
Classpath is used by compiler and jvm to find the class files, here “.” means current working directory and “PackageSample\*.class” means all class files from PackageSample folder.After we have to import that Sample.class file with the help of import keyword.
Now if we try to compile the Main.java it successfully creates PackageMain and also we can see that there is no Sample.class available in the current package directory
Run ->> java PackageMain.Main
CONCLUSION: JAVAC ALGORITHM FOR SEARCHING CLASS FILE
1) Searches class Sample{ } in current method as local inner class,if not available2) Searches class Sample{ } in class as class level inner class,if not available3) Searches for class Sample { } in current java file as outer class,if not available4) Searches for class Sample{ } in Current Package folder,if not available5) Searches for class Sample{ } in imported package,if not available
6) Throws error