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:
cd C:\JavaPrograms javac job\myCompany\myCompanyProgram.java java job\myCompany\myCompanyProgram
Or you can use periods as the directory separator: 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:
And you are over the C:\Games directory and type:
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 C:\Games, it will look in the next directory from the list, C:\JavaPrograms where it will found the job directory. Then it will look for the myCompany 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: java -classpath .;C:\JavaPrograms job.myCompany.myCompanyProgram
Setting the local CLASSPATH: - For DOS, W9X-WME: set CLASSPATH=.;C:\dir1;C:\dir2;C:\dir3 - For UNIX/LINUX systems: (note the list separator is : and not ;) bash shell: export CLASSPATH=.:/dir1:/dir2:/dir3 other shells (ksh, csh, sh) set CLASSPATH .:/dir1:/dir2:/dir3 (yes, without the '=' sign)
Setting the global CLASSPATH: - For DOS-W9X-WME Edit the c:\AUTOEXEC.BAT 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 /etc/.profile 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: echo %CLASSPATH% or print the contents of all the enviroment variables: set
- For UNIX/LINUX systems: print the content of the variable: echo $CLASSPATH or print the contents of all the enviroment variables: bash shell: export other shells (ksh, csh, sh) set
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 C:\JavaPrograms jar xvf job.jar job
(Type jar -h for more options)
And then distribute the file, wich can be executed this way: 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.