• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

classpath

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
I read chapter 10 the part about the classpath 2 times and I just don't get what a classpath is . I understand its a way of telling the compiler or jvm where to find other classes I need to compile or run my class. But I keep trying all different things and the compiler always seems to find them. Please help I don't understand the importance of classpath so could you explain it to me or give me links regarding that matter.
regards,
Sarah
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Ali wrote:
But I keep trying all different things and the compiler always seems to find them.



Well, what are some of the "all different things" have you been trying?

S Ali wrote:
Please help I don't understand the importance of classpath so could you explain it to me or give me links regarding that matter.



For small test programs, classpaths are not useful at all (hmmm. Maybe I can word this better). The compiler and JVM works fine with no classpath set for these programs. It is the larger programs, which have classes in different locations, or in separate jar files, that need to have classpath set.

Henry
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the following directory C:\work\com\dogthings



and in
C:\work


and from the command line
C:\work>javac dog.java compiles both dog and collar and doesn't complain about collar being in another package.
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

C:\work>javac dog.java compiles both dog and collar and doesn't complain about collar being in another package.



It doesn't complain, because there is nothing to complain about. The classpath specifies the root directory -- and in both cases, they are in the same root directory... or to be more specific, the "." directory. So, not specifying a classpath is fine here.

Henry
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I'm trying to get this inside my head so tell me if what I understand is true:
1- java and javac -classpath are both looking for .class files only.
2- I have to specify all sub directories of the directory I am in in the classpath explictly because it can only look for files that are not inside a subfolder.
3- It has nothing to do with package folders as long as the package root directory is in the same directory I am in.
4- I have to explictly type " . " for java if I have specified the -cp option otherwise it won't look in the same directory , althought for javac and if I don't use -cp in java its there by default.


 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Ali wrote:Okay I'm trying to get this inside my head so tell me if what I understand is true:
1- java and javac -classpath are both looking for .class files only.
Yes
2- I have to specify all sub directories of the directory I am in in the classpath explictly because it can only look for files that are not inside a subfolder.
yes
3- It has nothing to do with package folders as long as the package root directory is in the same directory I am in.
Yes
4- I have to explictly type " . " for java if I have specified the -cp option otherwise it won't look in the same directory , althought for javac and if I don't use -cp in java its there by default.
Yes but here is how javac and java looks for the class files needed.
java and javac by default searches JDK folder\jre\lib\ext then the classpath variable for the class files needed to compile. If classpath is not overridden anywhere(including command line option i.e using -cp) then it will look in to the current directory for the class files


 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Siva for your reply
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic