• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class path query

 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

OCP Java SE 6 Programmer practice exam --> practice exam 4 --> Question No 7


can any one explain me why answer D is wrong?

Thanks in Advance.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to compile option A is correct OK, then you need to run ...

now your question is why option D is incorrect.
from oracle documents:

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.


I strongly suggest you to compile as in option A then analyze the output ;)

For more detail: classpath

 
Himai Minh
Ranch Hand
Posts: 1256
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit Buddy,
java -cp pack.jar means java will search through the directories inside pack.jar to look for pack.client.Client. But this Client class is not in jar. The directory pack/client/Client is under $ROOT directory. Java needs to search $ROOT directory to find pack.client.Client.

Instead of D, G will work because java -cp .:pack.jar includes the current directory $ROOT.
 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman and Himay for your very helpful reply..

so what i understand is , when we want to run the class file, at that time we need that class file and the related class file that used by it.
so in this case, If i run with command "java -cp . pack.client.Client" that will not run because it can't find the bank class.

please , correct me if i am wrong.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect Ankit!
 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman and Himai.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic