aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpath 2 questions. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Classpath 2 questions." Watch "Classpath 2 questions." New topic
Author

Classpath 2 questions.

Jan Osykowski
Ranch Hand

Joined: Jul 15, 2010
Posts: 41
Hey guys,

Firstly, I would like to know when we specify the classpath, does it search only the pointed out directory (the files in it) or also do the recurrent search in the other directories that are placed in the specified one?
And also, how we can actually determine the root directory of the package? In the book it is written "Remember when using a classpath, the last directory in the path must be the super-directory of the root directory for the package". But if we have a package and inside it another package and so on, how we can actually determine which one is the root directory of the package and then further the super directory of the root directory for the package?
In the book we have an example:

And now I wonder. They say that myApp is a root directory of the package myApp.utils. But why? Why "ws" is not the root directory of the package ws.myApp.utils and the same with "test". Could someone please clarify this for me?

Cheers,
Jan.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

Firstly, I would like to know when we specify the classpath, does it search only the pointed out directory (the files in it) or also do the recurrent search in the other directories that are placed in the specified one?



yes it will serach only for pointed out directory


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

And now I wonder. They say that myApp is a root directory of the package myApp.utils.
But why? Why "ws" is not the root directory of the package ws.myApp.utils and the same with "test".
Could someone please clarify this for me?

because jar is created at myApp...the first directory contained in the jar is MyApp......
ws is not at all included in a jar then how come it be a root directory inside myJar jar......
Jan Osykowski
Ranch Hand

Joined: Jul 15, 2010
Posts: 41
But the .jar file here doesn't matter. Just forget about it. I'm asking in general.

Cheers,
Jan.
Rodmar Conde
Greenhorn

Joined: Jul 09, 2010
Posts: 11
And now I wonder. They say that myApp is a root directory of the package myApp.utils. But why? Why "ws" is not the root directory of the package ws.myApp.utils and the same with "test". Could someone please clarify this for me?


Hi Jan,

ws is not the root directory of ws.myApp.utils because that package doesn't exists. The example is only declaring the package myApp.utils (line 8 of the code). Then, to successfully locating classes in it you should be located at the directory in which myApp is directly accesible. The only place where this can happen is in directory ws.

Remember packages "reflect" a directory structure used to organize java classes. So if you code a class named Dates and declare it belongs to myApp.utils package what you're saying to the jvm is that the .class wil be located in a directory called utils that is inside a directory called myApp. So it's your responsibility to make that directories accesible to the jvm. So, the only ways to correctly load the Dates class are:

Inside out of directory test:
java -cp .\test\ws myApp.utils.Dates

Inside directory test:
java -cp .\ws myApp.utils.Dates

Inside directory ws:
java myApp.utils.Dates

Note that, inside directory myApp, if you try:
java myApp.utils.Dates
or
java utils.Dates

An error will be thrown... since myApp directory is not accesible in the first case and there is no class Dates defined to belong to a package named utils in the second case.

Hope I clarify your doubts.




SCJA | SCJP Semi Study Guide in Spanish
Jan Osykowski
Ranch Hand

Joined: Jul 15, 2010
Posts: 41
Hola Rodmar!

Yes, now it became very clear! Thanks a lot.

Cheers,
Jan.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Classpath 2 questions.
 
Similar Threads
chapter 10 -->page 804-->classpath ???
K&B Chapter 10 Self Help Question 9
question about classpath
Question on Classpaths
SCJP chapter 10 : Development