Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what exactly setting a classpath means?

 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
any one of u please give me the explanation of what setting a classpath exactly means?
after writing a class and putting it inside a package, i am not able to run the class from the place where i had stored it.for example if my class named xyz is in c:\mypack\xyz, i am not able to run the file frm the same location.i am able to run the file only from c:,is it anythg related to setting the classpath?
regards,
raja
 
Anil Kumar Saha
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see http://www.kevinboone.com/PF_classpath.html
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classpath is an environment variable which the jvm uses for finding a particular class. The current directory is default in the classpath

for eg. what in windows do u do to find a file?...

for eg>
suppose u are having file A.java in d:\test1\
suppose u are having file B.java in d:\test2\
thus if u are using file B in A may b by creating Object of B in A. u will need to have B in classpath of A. So, that when u try to compile file A, jvm can see where the file B is...

command on windows may b like this : set classpath=%classpath%;d:\test1

where %classpath% is used to include the existing classpath so that it dosent get overriden..
[ March 07, 2005: Message edited by: Nischal Tanna ]
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Raja,

As for myself, I would prefer to use the -classpath (or simply -cp) javac compiler option to set the classpath.

If you plan to develop several applications, its easy to understand that managing the classpath through your environnement variable, will lead to a very large search path.

This is well explained in the article recommanded by Anil Kumar Saha.

Hope that helps,
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of link's sugested error msg
more, if i try:
doesnt compile, oposite to what author states
finally, i tried:
and again canyt compile Test2
what am i doing wrong?
thanks in advance


i had saved test2 as...txt

[ March 08, 2005: Message edited by: miguel lisboa ]

edited again
but, in the end, how does one runs the app?
thanks in advance
[ March 08, 2005: Message edited by: miguel lisboa ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miguel,

What you have to realize about this article is that the author starts out by giving you "how to do it wrong" so that you hopefully will have an understanding about how it works.

How to do it right is:
  • Both when compiling and when running a class, set your current directory to the root directory from which your folder tree branches out. In your case, you should set the current directory to c:\raiz.
  • Both when compiling and when running a class, Always include the current directory in your classpath (-cp = ".") along with whatever other classes you need.


  • the following should compile both classes:


    When running the class, use the following:


    Having said this, realize that for large java projects, this isn't a very convenient way to compile. Most developers will either use ANT, which is an open source build tool, or they will use an Interactive Development Environment (IDE) such as Eclipse or NetBeans that compiles for them.
    [ March 08, 2005: Message edited by: Merrill Higginson ]
     
    miguel lisboa
    Ranch Hand
    Posts: 1281
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i did it

    i'm using eclipse and was totally forgotten about command line
    i should use cp instead of classpath
    thanks for the good help
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic