1) The package mess:
Say you have a JavaPrograms directory for your java sources. If you place all your codes in there it will be a mess sooner or later. Just like your documents directory. So you sort your codes in different directories, job, school, fun, etc. But how do you compile and run your codes? It would be nice to compile and run every code from the JavaPrograms directory, so the JVM let you do that adding a package statement to your codes. (off course it is not the only motivation/adavantage of this feature).
This way you can compile and run your codes from the JavaProgram directory:
Code:
cd C:\JavaPrograms
javac job\myCompany\myCompanyProgram.java
java job\myCompany\myCompanyProgram
Or you can use periods as the directory separator:
Code:
java job.myCompany.myCompanyProgram
2) The CLASSPATH mistery:
Now, what if I don't want to be in the C:\JavaPrograms directory to run my java classes. Well, the JVM can access an enviroment variable called CLASSPATH, which have a list of directories where it can find Java classes. So if this variable has the following list:
Code:
CLASSPATH=.;C:\JavaPrograms
And you are over the C:\Games directory and type:
Code:
java job.myCompany.myCompanyProgram
The JVM will look in the first directory of the CLASSPATH list ( . which means the current directory) for the job directory. As it can't found the job directory in
, it will look in the next directory from the list,
where it will found the job directory. Then it will look for the
directory and inside it for the myCompany class, which then is executed.
All directories specified in the CLASSPATH list must start from the filesystem root <absolute path> (DOS-WIN: C:\ - UNIX: /).
But, How do I set the CLASSPATH variable?
It depends on the OS. For every OS, the CLASSPATH as any other variable has a scope. It could be global or local. If you set it in a console, it will have local scope, so the commands typed in that console will see it. If you set it globally, all consoles opened in the system will see the variable. Also you can give the CLASSPATH for a single java command with the java option -classpath or -cp in any OS:
Over any directory:
Code:
java -classpath .;C:\JavaPrograms job.myCompany.myCompanyProgram
Setting the local CLASSPATH:
- For DOS, W9X-WME:
Code:
set CLASSPATH=.;C:\dir1;C:\dir2;C:\dir3
- For UNIX/LINUX systems:
(note the list separator is : and not

bash shell:
Code:
export CLASSPATH=.:/dir1:/dir2:/dir3
other shells (ksh, csh, sh)
Code:
set CLASSPATH .:/dir1:/dir2:/dir3
(yes, without the '=' sign)
Setting the global CLASSPATH:
- For DOS-W9X-WME
Edit the
file and add the CLASSPATH declaration for a local variable.
- For wXP-2K:
You can set the CLASSPATH in the system properties. To set it you must open the control panel and double click System (or type the windows key <between ctrl and alt> and the pause key at the same time). There you must go to advanced and Enviroment variables. There is a list with all the enviroment variables, including the CLASSPATH. If it is not present you can create it.
- For UNIX/LINUX systems:
It depends on the shell used. basically you can set it in the
file using the declarations for local variables, also you can set it in the local .profile file of each user. Check your system documentation for information on "how to set enviroment variables".
And, how do I view the CLASSPATH content?
- For DOS, W9X-WME-NT-2K:
print the content of the variable:
or print the contents of all the enviroment variables:
- W-NT/2K users can find more information here:
XP users can check the variable list in the System properties as explained above.
- For UNIX/LINUX systems:
print the content of the variable:
or print the contents of all the enviroment variables:
bash shell:
other shells (ksh, csh, sh)
3) The JAR stuff
If you want to deploy an application, it would be nice if you dont have to create the whole directory structure to run your program. So you can create a jar file (a kind of zip file) containing the root directory of your application and include it in the CLASSPATH:
in
Code:
C:\JavaPrograms
jar xvf job.jar job
(Type
for more options)
And then distribute the file, wich can be executed this way:
Code:
java -classpath C:\deployDirectory\job.jar job.myCompany.myCompanyProgram
The jar could be included in the CLASSPATH variable too. And you can include any jar you may need, as you can include any directory you may need.
You can find more information about jar files here:
You can find more information about the standard package names here:
You can find a detailed explanation of this whole issue here (recommended):
Well, I really hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.