This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java classpath

 
aakash bhatt
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we define . or .; in Java classpath environment settings at the start
 
Alex Sack
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you need some classes that are located relative to your current execution wd to start the application
asac
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asac,
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.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
%SET CLASSPATH = ${CLASSPATH}:.
(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 . MyClass.java
There is of course more to the story than this, but if you're just starting out, this should work fine.
cheers,
Kathy

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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathy,
You may like to talk a little about the -cp and -classpath switch as well.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic