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 Interesting classpath question 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)
Bookmark "Interesting classpath question" Watch "Interesting classpath question" New topic
Author

Interesting classpath question

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Guys,

After practice a lot of questions about classpath, I created the below question by myself and would like to share with you.

I don't know if questions like the below one are on the exam, anyway, I think it still is a good question.

Here we go ...

For the question below consider using the Java version 1.5

Given the directory structure :



And these two files :



And the two command lines from within source directory :

javac -source 5 com/scjp5/CustomMessageMain.java
java -classpath . com.scjp5.CustomMessageMain

Which version of CustomMessage.class file will be used when run the code :

a) The class version within source/com/scjp5/classes folder

b) The class version within My_Custom_Classes.jar file

c) The CustomMessageMain.java file will never compile because a compile error.

d) The CustomMessageMain.java file will never compile because
CustomMessage.class file cannot be found.

e) A NoClassDefFoundError exception will throw at runtime


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
if you set CLASSPATH like this :
.; $JAVA_HOME/jre/lib/ext/My_Custom_Classes.jar;.....;

javac -source 5 com/scjp5/CustomMessageMain.java
java -classpath . com.scjp5.CustomMessageMain

the CustomMessage.class from com.scjp5.classes package is used both in complie time and run time
if you set CLASSPATH like this :
$JAVA_HOME/jre/lib/ext/My_Custom_Classes.jar;.....;.;
In the complie time the CustomMessage.class from My_Custom_Classes.jar is used
In run time the CustomMessage.class from com.scjp5.classes package is used


Which version of CustomMessage.class file will be used when run the code :

I think the a is corret(maybe in the run time )

if l mistake plz correct me!

[ March 18, 2006: Message edited by: Changchun Wang ]
[ March 18, 2006: Message edited by: Changchun Wang ]
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
B
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
could you explain it clearly ?
in complie time?in run time ?which version is used?
thans in advance!
[ March 18, 2006: Message edited by: Changchun Wang ]
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Even if the classpath is

.; $JAVA_HOME/jre/lib/ext/My_Custom_Classes.jar;.....;

or

$JAVA_HOME/jre/lib/ext/My_Custom_Classes.jar;.....;.;,

the class in My_Custom_Classes.jar will be used both at compile time and at runtime.

The java compiler and the java runtime will look for CustomMessage.class in the directories listed in CLASSPATH chronologically. In both the cases, $JAVA_HOME/source/com/scjp5/classes does not fall under the CLASSPATH. If the CLASSPATH was, say,

.;$JAVA_HOME/source/com/scjp5/classes;$JAVA_HOME/jre/lib/ext/My_Custom_Classes.jar;.....;

then CustomMessage.class in $JAVA_HOME/source/com/scjp5/classes would have been used.

regards,
vijay.
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Sorry, the final CLASSPATH should actually be

.;$JAVA_HOME/source/;$JAVA_HOME/jre/lib/ext/My_Custom_Classes.jar;.....;

regards,
vijay.
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
from Edisandro Bessa

javac -source 5 com/scjp5/CustomMessageMain.java
java -classpath . com.scjp5.CustomMessageMain

we know the current package is /source.
maybe Edisandro compile and run CustomMessageMain like this


in the current package(/source), there exists the CustomMessage.class in the com/scjp5/classes package

so if the classpath is

.;$JAVA_HOME/source/;$JAVA_HOME/jre/lib/ext/My_Custom_Classes.jar;.....;

I think the CustomMessage class in /source/com/scjp5/classes packageis used both in compile time and run time
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
Edisandro
could you supply your classpath to us?
thanks!
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Guys,

First of all. The correct answer is B.

So let me explain why.

  • 1st -> In order to find all .class files they need, both java and javac commands use the same search mechanism, so, in this question, both commands will use the same .class version. (At compile and run times)
  • 2nd (Most Important) -> Inside your Java installation directory (which I called as $JAVA_HOME, maybe you can find questions on the real exam that refers to java instalation directory as $JAVA_HOME), there's a subdirectory named jre/lib/ext. If you put any custom .jar files into the ext subdirectory, java and javac can find the classes they contain even you don't mention these .jar files in your classpath.
  • 3rd -> Actually, there's a core function into Java that performs the search in .jar files located in jre/lib/ext directory BEFORE look at any classpath provided at command line or system variable. So, in the same way the java and javac commands look from left to right at directories provided in classpath and stop searching when the first occurrence of file is found, java and javac commands also ignore any classpath if the .class files they need is found at .jar files existing in jre/lib/ext subdirectory.
  • 4th -> I got surprised when I first saw the main declaration public static void main (String ... args) as a valid signature for main method. So, I decided to put this kind of main declaration just to confuse some guys who don't know this issue.


  • Note that String ... args as command line parameter is valid only as of Java 5.
  • 5th -> In javac -source 5 com/scjp5/CustomMessageMain.java command, there's no need to put the argument -source 5. I put -source here just to explicitly tell that I want compile my code for Java 5 because of using a different main method signature.
  • 6th -> In java -classpath . com.scjp5.CustomMessageMain command, the custom classpath provided tells Java to searching inside source/ subdirectories for any .class file it need. However, DON'T forget that Java first looks at jre/lib/ext subdirectory in order to find the .jar files and classes it needs. So, if Java is able to find the file inside jre/lib/ext subdiretory, it simply ignores any entries provided in classpath.


  • Bottom line, DON'T forget that regardless of entries provided in classpath, java and javac command will ignore them if they're able to find the .class file they need inside some .jar file existing right into $JAVA_HOME/jre/lib/ext.


    Just for your information, ext means external. I would say, external classes.

    Please let me know if you need some more explanations about this question.

    Edisandro.
    Aleksander Zielinski
    Ranch Hand

    Joined: Nov 11, 2005
    Posts: 127
    Thanks Edisandro for interesting question, it's always good to try myself especially that exam is coming closer. Sorry, I told you I'd let you know in the evening, but I cannot send you another private mesage until you reply, so I please send a short reply.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Interesting classpath question
     
    Similar Threads
    K&B(SCJP5) page no:769.......
    Passed SCJP5 finally
    classpath problems compiling on Windows 2000
    I think the answer should be "B" ? K&B SCJP5 errata
    import & package