Classpath
Classpath
Compiling a java file having reference to a class present in different directory but default package
- Main.java present in C:\BZ_Coder_Training\Java_programs
- Contains a reference to Employee.class present in C:\BZ_Coder_Training\test
To compile Main.java
javac -cp C:\BZ_Coder_Training\test Main.java
To run Main.class
- To run Main class also we need to provide the classpath using -cp but when we use -cp flag java will
only look for classes in that classpath even for Main.class. If we use
java -cp C:\BZ_Coder_Training\test Main
then java will look for Main.class in C:\BZ_Coder_Training\test instead of looking into cwd, so we need to
provide two classpaths which will be separated by ;
java -cp C:\BZ_Coder_Training\test;C:\BZ_Coder_Training\Java_programs Main
Compiling a java file having reference to a class present in different directory but in some package
- Main.java present in C:\BZ_Coder_Training\Java_programs
- Contains a reference to Employee.class present in C:\BZ_Coder_Training\test in com.bzcoder.test
To compile Main.java
use import statement in Main.java as import com.bzcoder.test.Employee
then compile like below
javac -cp C:\BZ_Coder_Training\test Main.java
To run Main.class
- To run Main class also we need to provide the classpath using -cp but when we use -cp flag java will
only look for classes in that classpath even for Main.class. If we use
java -cp C:\BZ_Coder_Training\test Main
then java will look for Main.class in C:\BZ_Coder_Training\test instead of looking into cwd, so we need to
provide two classpaths which will be separated by ;
java -cp C:\BZ_Coder_Training\test;C:\BZ_Coder_Training\Java_programs Main