File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain the behaviour of classpath while using in javac using the below example Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Explain the behaviour of classpath while using in javac using the below example" Watch "Explain the behaviour of classpath while using in javac using the below example" New topic
Author

Explain the behaviour of classpath while using in javac using the below example

vvus bharadwaj
Ranch Hand

Joined: Apr 28, 2012
Posts: 38
I have B1.java file in pack2 directory.I have A21.java file in pack1 directory.The directory hierarichy is as follows
pack2|
B1.java
pack1|
A21.java
The code is as below:


Now i compiled the A21.java file successfully(i am in pack1 directory).I understood this one
Now i turned to pack2 directory and compiled B1.java and it is also successfully compiled using "javac B1.java.I did not understand why it has been compiled because i did not specify any classpath to locate for A21.java(B1.java uses A21.java) in the command line then My question

1)How the complier knows the A21.java is in pack1 directory.I assume that complier looks for the current directory for source file and other files.Moreover iam in current directory pack2 so it complied success fully .Is this right?Please tell me how classpath can be used in locating other files in javac command i.e tell me the sequence of steps while we use classpath in javac?

Since the source file complied successfully I thought to run the code using "java B1" ( iam in pack2 dirctory only).Then it shows java.lang.NoClassDefFoundErrorException.My Question is:

2)Why it showing the exception.Please give me the correct command.Also explain sequence of steps while we use classpath in java.

Explain when classpath should be used in both javac and java commands?

Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Is the parent directory of pack1 and pack2 on the classpath?

As for running the code, you need to run it from parent directory of pack2. So lets say your directory structure is this


Then generally to avoid any confusion you should use these commands to compile and run your code
c:\parent>javac pack1\A21.java
c:\parent>javac pack2\B1.java
c:\parent>java pack2.B1

Basically when you run a class you have to specify its fully qualified class name including the package. Even if you specify the fully qualified class name and try to run the command like this
c:\parent\pack2>java pack2.B1
This won't work either as you are in pack2 directory already so java will look for another directory named pack2 inside of your current directory and then look for B1.class in it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
vvus bharadwaj
Ranch Hand

Joined: Apr 28, 2012
Posts: 38
My directory structure is like this.
Parent|
pack2|
B1.java
pack1|
A21.java



In the above problem I used A21 class in B1 class.I complied the file(i am in pack2 directory) without using classpath.Why it is compiled because i do not specify and search path to locate the A21.java?This is my problem.
After compiling i run the file using java B1(i am still in pack2 directory).Then it results in NoClassDefFoundErrorException:B1(wrong name:pack2/B1).Why?compiler able to see the class A21 and B1 but why JVM cannot able to see the B1 .Please tell me the reason and correct answers.

Also explain how the javac and java will look for the required classes i.e where they will start and where they will end the searching taking the case of with and without using classpath so that i can avoid the confusion regarding classpaths.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 319
vvus bharadwaj wrote:After compiling i run the file using java B1(i am still in pack2 directory).Then it results in NoClassDefFoundErrorException:B1(wrong name:pack2/B1).Why?

Because the name of your class is pack2.B1, so you have to run it as
java pack2.B1
and the directory that your pack2 directory is in (the one that contains B1.class) has to be in your classpath.
vvus bharadwaj
Ranch Hand

Joined: Apr 28, 2012
Posts: 38
1)I want to make sure that classpaths can be used only when we put classes into the packages.

2)You said that the path to the pack2 directory must be in the classpath.Can i set that path via command line?Please tell me the correct classpath for that?

3)Does javac command defaultly search for the current directory for .java files and class files it requires?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 867

vvus bharadwaj wrote:1)I want to make sure that classpaths can be used only when we put classes into the packages.

2)You said that the path to the pack2 directory must be in the classpath.Can i set that path via command line?Please tell me the correct classpath for that?

3)Does javac command defaultly search for the current directory for .java files and class files it requires?


You said that you are in pack2 directory and you executed the command javac B1.java it worked successfully i.e. it compiled B1 class successfully. this cannot be true(according to the structure you have specified). B1 extends B2 and B2 extends A21. now B1 class can access B2 class since B1 is in the same package as B2 and B2 has got default access. also since you are in pack2 , javac will be able to find B1, as well as B2 . but it won't be able to find A21 because A21 is in different package viz pack1. so the compilation will fail. for successfull compilation pack1 should be in classpath like this :

Suppose you are in pack2 directory(which you already are and as you said you have successfully compile A21 file) then to compile B1.java the command will be like this(remember you are in pack2)

javac -classpath ../ B1.java

Above command will put the parent directory on the classpath(../ refers to parent of the current directory, but then you already knew this) from where it will be able to find A21.class.

Regarding your 3 question, yes javac and java binaries by default search for files in the current directory if the classpath variable is not set. however if you set classpath variable either through environment variable or you set it through command line , then that default path will be overridden by the path you set and you would have to explicilty include current directory if you want it i.e. the current directory to be in your classpath.

I hope this clears everything.


OCPJP 6(100 %) OCEWCD 6(91 %)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain the behaviour of classpath while using in javac using the below example
 
Similar Threads
error while compiling
Package still not working
doubt in -cp option....
Explain the classpath when used with javac?
Compiling java files in different packages with single javac command