Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Java  classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java  classpath" Watch "Java  classpath" New topic

Java classpath

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
Why do we define . or .; in Java classpath environment settings at the start
Alex Sack

Joined: Jul 14, 2003
Posts: 5
because you need some classes that are located relative to your current execution wd to start the application
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
As asac implied, the . specifies the current working directory is to be included in the CLASSPATH.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
Howdy --
Both the compiler and the JVM need to find any of the classes that you're using in your code. If those classes are from the standard edition API, then no problem -- both java and javac know where *those* are. But they don't know about your *own* classes... the ones you're writing.
So... if you have a class that you just wrote, in a directory "foo", and the class is NOT in a package, then you cd to directory "foo" and then compile from that directory. That's the simplest way, to start out with. Including the dot "." in your classpath is the way you tell they system to "when you're looking for classes, be sure to look in the current directory (i.e. the one you cd to)."
Where to put that "."? You can add it permanently to your classpath as an environment variable in DOS (via autoexec.bat, if you're still on Windows 98!), or a property in Windows NT (I think -- can't remember it's been so long since I've seen NT). Or if you're on some flavor of Unix (including Mac OSX), you can put it in one of several places, including your .cshrc file (or whatever your initialization file is for your particular shell).
That's my preferred way... I never have to think about the current directory that way. (And you can still override that if you do NOT want the current directory to be included, which is sometimes necessary with RMI, but that's another story...)
Or, you can make sure that you have the "." by using the -classpath flag with java and javac:
java -classpath . MyClass
Or, you can do it just for your current terminal session, by using whatever it is you have to do for your particular shell. Something like:
(this says, "Take the current classpath, and add the current directory (.) to it, and then make that the new classpath setting for as long as this terminal is active.")
%java MyClass
(this is not necessarily *real* syntax)
If you use packages, it gets a little more complicated, although you'll still need the "." on the classpath.
What has troubled some programmers is that they are using an IDE which makes all kinds of settings for the classpath, and which may NOT include the current directory. That's when it's probably easiest to just use the -classpath flag:
javac -classpath .
There is of course more to the story than this, but if you're just starting out, this should work fine.

Java technology is everywhere -- in mobile phones, cars, cameras, printers,
games, PDAs, ATMs, smart cards, gas pumps, sports stadiums, medical devices, Web
cams, PCs, servers, you name it. If you develop software and haven't learned
Java, it's definitely time to dive in -- Head First.

- Scott McNealy, Sun Microsystems Chairman, President and CEO
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

You may like to talk a little about the -cp and -classpath switch as well.

Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
I agree. Here's the link:
subject: Java classpath
It's not a secret anymore!