• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interesting classpath question

 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edisandro
could you supply your classpath to us?
thanks!
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 127
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic