Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lost on the classpath!

 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help understanding this classpath thing. Looking ahead at OOP1, I see instructions telling us we have to download a .jar file, and then something about the classpath stating that if you write your programs in C:\java ( and I do ) then the .jar files could be put in a folder in C:\java called somenameorother and the classpath would look like:
C:\java;C:\java\somenameorother

what exactly is going on in this classpath statement? Why the semi-colon in the middle? I never really had to learn any DOS commands, so I get lost over the simplest things! Does the above mean that the files will be found in C:\java and C:\java\somenameorother? Can classpath be that simple? Would file location be another way to say classpath, or I am missing some occult meaning? Another sleepless night..................
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The semicolon is a separator. The classpath is to tell the program you are running (javac I this case) where to look.
I'm not sure of the exact sequence (I'll check today & post correct later if I'm wrong), but I think it goes something like this, first the directory your program is in, then the first directory in the classpath, then the next etc.
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The CLASSPATH tells the Java compiler where to look for Classes. It contains file locations (pathnames). The format to define it is based on the DOS methodology for creating an environment variable on a Windows PC. I'm not sure what the format would be on the UNIX or MacOS operating systems, but the idea is the same.
javac search order for Classes:

    1. The current directory.
    2. $JAVA/classes (where $JAVA is the parent location of the Java compiler, interpreter and other components).
    3. $JAVA/classes.zip. This is a .zip file rather than a directory. Java knows how to extract class library components from a .zip file.
    4. Any additional directories assigned to the environment variable (CLASSPATH)

    Just Java 2, Peter van der Linden, discusses this topic on page 223.
    Michael
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew I saw it somewhere thanks saves me looking
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CLASSPATH is also discussed briefly on the first cattle drive page.
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still don't get it. How do I determine what my classpath is? I created a folder on my C drive ( called the root (?) ) named java. I put my JDK in this folder, and this is where I save all of my java and class files for the cattle drive. So what would my classpath be if I downloaded the necessary .jar files into
C:\java? For some reason this is not making any sense to me. If I download a .jar file and place it in C:\java, will the compiler be able to locate it without any specific instructions?
When I installed the JDK it worked without my having to monkey with any of this stuff!
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol at the dos prompt type in set and you will see a list all you settings.
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll do that!
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, for my PATH I have the following:
PATH=C:\WINDOWS;c:\windows;c:\windows\COMMAND;C:\JAVA\BIN
So what does all that mean? I don't have any experience with DOS commands, so this is Greek to me! And I don't speak Greek!
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definition: A class path is an ordered list of directories or *.zip or *.jar files in which to search for class files.

Therefore, if you want a *.jar file in your classpath, you have to explicitly put it there, separating it from the previous classpath with a semicolon.

For example,
CLASSPATH=C:\Java;C:\Java\example.zip;C:\Java\example2.jar
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I download the necessary.jar files and put them in my
C:\java folder which is where I keep all of my java files, then my CLASSPATH would look like:
C:\java;C:\java\necessary.jar
In order for the compiler to find the class files that are located in the .jar file?
Sorry to be so dense, but there is something about this which is keeping me from feeling like I understand what everyone is talking about.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So if I download the necessary.jar files and put them in my
C:\java folder which is where I keep all of my java files, then my CLASSPATH would look like:
C:\java;C:\java\necessary.jar
In order for the compiler to find the class files that are located in the .jar file?

Exactly
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on your operating system, UNIX or e.g. Windows NT,
you are able to set your CLASSPATH environment variable.
UNIX uses e.g. '.profile' (if using K-shell) (for C-shell '.cshrc', and maybe other shell's use other files to execute and set environment variables first ['.bashrc']).
On windows NT I right-click 'My computer' (on the desktop) and open the tabpage for Environment, then I goto 'CLASSPATH' (and click) and then I can add or change the CLASSPATH (don't forget to "set" it too !).
And as is mentioned above the semicolon is a necessary separator if you have to point to different jar-files or directories.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic