This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain the classpath when used with javac? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

Explain the classpath when used with javac?

vvus bharadwaj
Ranch Hand

Joined: Apr 28, 2012
Posts: 38
I have one directory called mypack.In side that i have some java files and i have a package called pack2.This pack2 contain some java files which belong to pack2 and other package called pack1.pack1 contains java files which belong to pack1.Now my Question is "How to compile the classes which require other classes in other package using classpath?"This is the example i am giving?
1)
package pack2;
import pack1.*;
public class B1 extends A21{}

package pack1;
public class A21{}

For this I compile the B1 class successfully using javac B1.java(A21 successfully compiled)
Why there is no need to use the classpath here?

2)
package pack2;
//import pack1.*;
public class B1 extends A21{}

package pack2;
public class A21{}
If i tried to compile the B1 class(javac B1.java) i am getting the error .Why?To compile successfully this one command is javac -classpath ../. B1.java

Please explain how compiler follows the classpaths.Explain me the rules when we compile the classes using the classpath i.e from where compiler starts looking for .class files and its progress till it finds?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

vvus bharadwaj wrote:I have one directory called mypack.In side that i have some java files and i have a package called pack2.This pack2 contain some java files which belong to pack2 and other package called pack1.pack1 contains java files which belong to pack1.Now my Question is "How to compile the classes which require other classes in other package using classpath?"This is the example i am giving?
1)
package pack2;
import pack1.*;
public class B1 extends A21{}

package pack1;
public class A21{}

For this I compile the B1 class successfully using javac B1.java(A21 successfully compiled)
Why there is no need to use the classpath here?

2)
package pack2;
//import pack1.*;
public class B1 extends A21{}

package pack2;
public class A21{}
If i tried to compile the B1 class(javac B1.java) i am getting the error .Why?To compile successfully this one command is javac -classpath ../. B1.java

Please explain how compiler follows the classpaths.Explain me the rules when we compile the classes using the classpath i.e from where compiler starts looking for .class files and its progress till it finds?



i think the information you have given won't be sufficient to answer your question. however i can give you insight regarding the concept. hope it helps. lets begin. firstly the default place where javac, java looks for classpath is in the current working directory. this is important , if your classpath environment variable is not set then the default place where the executables look for class files is in current directory. if you have set your classpath environment variable , either through windows environment variable or through command prompt it will override the default value and jdk binaries will look in the path you have specified. the important point here is that if you want your current directory to be searched then you have to include it in your classpath. this way of specifying classpath however is not recommended way. the recommended way is to specify the -classpath or -cp option. the benefit of this is that you can customize your classpath according to your application rather than relying on one single class path. the -classpath or -cp switch takes as arguments list of directories separated by : . Again it overrides the default value of current directory so you have to explicitly include current directory if you want javac, java to scan your current directory. for complete process how jdk binaries search for classes is very well explained on oracle site. the jar containing standard java classes is searched first. that is how you are able to use java system classes in your program without specifying classpath for them. also there is this java extension mechanism. the directory path jre/lib/ext is searched for any JAR file . Warning - extension mechanism is only for jar file. it won't search for any class file you place there.
Also there are other subtle details which are covered in kb6 book. i can't explain the whole chapter here but i think above should suffice.

what i would recommend is to practice self test questions . they are really good and they will help you understand every bit about classpath. also in your similar post i have given a link on ibm developer works site. it is really good. have a look at it.

coming to your question, in first scenario you said javac B1.java compiled successfully(A21 being successfully compiled). in this case what was your classpath ? had you set any classpath environment variable or not ?


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
vvus bharadwaj wrote:I have one directory called mypack.In side that i have some java files and i have a package called pack2.This pack2 contain some java files which belong to pack2 and other package called pack1.pack1 contains java files which belong to pack1.Now my Question is "How to compile the classes which require other classes in other package using classpath?"This is the example i am giving?
1)
package pack2;
import pack1.*;
public class B1 extends A21{}

package pack1;
public class A21{}

For this I compile the B1 class successfully using javac B1.java(A21 successfully compiled)
Why there is no need to use the classpath here?

2)
package pack2;
//import pack1.*;
public class B1 extends A21{}

package pack2;
public class A21{}
If i tried to compile the B1 class(javac B1.java) i am getting the error .Why?To compile successfully this one command is javac -classpath ../. B1.java

Please explain how compiler follows the classpaths.Explain me the rules when we compile the classes using the classpath i.e from where compiler starts looking for .class files and its progress till it finds?


Hi, VVus,
In case 1 ), I have a feeling that you are in pack2's current directory. When you tried to compile B1, the javac looks for pack1 which is under pack2 directory. Since javac finds pack1, it compiles.
In case 2), Is A21 in pack1? You commented out import pack1.*; statement. It won't compile because A21 is not defined in pack2. If you uncomment out the import statement and if pack1 is outside pack2, then java -classpath ../. B1.java may work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain the classpath when used with javac?
 
Similar Threads
error while compiling
packages and classpath! or at least i guess!
Issue when compiling a class which is importing a package
Classpath
Compiling java files in different packages with single javac command