aspose file tools*
The moose likes Beginning Java and the fly likes what exactly setting a classpath means? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what exactly setting a classpath means?" Watch "what exactly setting a classpath means?" New topic
Author

what exactly setting a classpath means?

rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
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

Joined: Apr 07, 2004
Posts: 111
see http://www.kevinboone.com/PF_classpath.html


Regards,

Anil Kumar Saha
SCJP 1.4
http://www.agilej.blogspot.com/
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
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 ]

Thnx
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
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,


Lionel Badiou
CodeFutures Software
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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 ]

java amateur
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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 ]

    Merrill
    Consultant, Sima Solutions
    miguel lisboa
    Ranch Hand

    Joined: Feb 08, 2004
    Posts: 1281
    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
     
    Consider Paul's rocket mass heater.
     
    subject: what exactly setting a classpath means?