Francis Zabala wrote:
I have set the classpath in windows 7 as so:
Don't do this. Just remove it. You should set your PATH to this location which helps the executables like javac/java etc... run from anywhere.
When you run a Java program, by default Java uses the current directory (if you have not set the classpath explicitly) to find the classes it needs. If you need other third party classes etc... you may set them in the classpath for your convenience. Read How To Set The Classpath to see how you can do this.
I'm afraid I am no expert on this since I do everything in an IDE... messing around trying to get everything in the right folders can be a headache as you have found... Maybe some others could chime in with detailed answers to your questions.
You need to look up package statements and how they relate to directory/folder structures. Each name in a package refers to one directory/folder level by that same name.
Every Java class is in a package. The default package has no name, and classes in it are expected in a root directory of one element of your classpath.
So you can have a class named User, with no package, and put it in whatever directory you want. To access that class, either the classpath needs to specify the directory, or the classpath specifies "." and the directory can be your default directory (i.e., the one you have "changed directory (cd)" to on the command line.
For a package example, if your class user is in the package xcom.innerPackage, then you can have a classpath that includes the value "C:\Users\francis\Dropbox\javaTemp", and the class in xcom\innerPackage\User.class within the javaTemp directory.
Or you can have your default directory be C:\Users\francis\Dropbox\javaTemp, put "." in your classpath (if it isn't a default, I can never remember), and have User in the same location.
Yes, as near as I can tell without trying it, these are correct -- assuming User is a class with a main method in it, so Java can run the class directly.
You could also enter
instead of putting "-cp <value>" on the command line.
Sorry for the dense explanation -- without writing a small chapter for a book, I can't cover the subject completely (we haven't mentioned jar files yet); if you need that much explanation, there are better places to get it than here.
Yes, you got it right. Congratulations on sticking with it.
I'm now going to extend the lesson one more step. If it confuses you, you can read more about it in all kinds of references.
The classpath can contain more than one item; each item points to a root of classes. It can be a directory, like you have been using, or it can be a jar file.
A jar file is a zip file with java classes and maybe some other things in it. As you may know, zip files store the directory path as well as the name of each file stored in that file. So a jar file is created from a set of classes in a directory tree that maps the package structure, just like the (small) directory tree you've been using.
To put an additional item on a classpath, you separate it with with a semicolon in Windows, a colon on Unix. Let's say you had a communications library written in java and distributed as a jar file named comm.jar. You could have:
That's assuming the jar file is in the current directory, and the other classes you want to use are rooted at the current directory. The java runtime, when it needs a class, starts with the first item on the classpath, then goes through them one at a time looking for the classfile it is trying to find.
So I hope that's a useful addition. I thought it worthwhile to mention it while you were finishing up the topic.
Thanks for the input guys! Really appreciate it. I don't have anything against setting the classpath using the environment variables, it's that I like to use the -cp option every time so that I can practice using it and when I want to test my codes on different machines. That's why it's in my dropbox folder.