Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about Packages, the CLASSPATH and jar files

Starting out with Java

about Packages, the CLASSPATH and jar files

by  pedrosolorzano  Posted    (Edited  )
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
Code:
C:\Games
, it will look in the next directory from the list,
Code:
C:\JavaPrograms
where it will found the job directory. Then it will look for the
Code:
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:
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
Code:
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
Code:
/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:
Code:
echo %CLASSPATH%
or print the contents of all the enviroment variables:
Code:
set

- W-NT/2K users can find more information here:
http://www.microsoft.com/mspress/books/sampchap/4127.asp#162
XP users can check the variable list in the System properties as explained above.

- For UNIX/LINUX systems:
print the content of the variable:
Code:
echo $CLASSPATH
or print the contents of all the enviroment variables:
bash shell:
Code:
export
other shells (ksh, csh, sh)
Code:
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
Code:
C:\JavaPrograms
jar xvf job.jar job

(Type
Code:
jar -h
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:
http://java.sun.com/docs/books/tutorial/jar/index.html

You can find more information about the standard package names here:
http://java.sun.com/docs/books/tutorial/java/interpack/createpkgs.html

You can find a detailed explanation of this whole issue here (recommended):
http://java.sun.com/docs/books/tutorial/java/interpack/managingfiles.html
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top