Page 792 - Introduction to Programming with Java: A Problem Solving Approach
P. 792
758 Appendix 4 Packages
In a Windows environment, the appropriate way to add a class path to the operating system’s environment is to go to the Control Panel icon and click System. Then under the Advanced tab click Environment Variables. . . .Then select CLASSPATH and click Edit. . . .Add a semicolon to the end of the list, and then enter your desired new class path, for example, C:/myJava. In UNIX, use the setenv command, like this:
setenv classpath .:/myJava
Some Advanced Options
Optionally, you can ask the Java compiler to put the compiled .class file into your desired destination di- rectory automatically. To do this, invoke the compiler from a command prompt with the -d option, like this:
javac -d <class-path> <source-code> For our Car example, this would be:
javac -d E:/myjava Car.java
The full pathname of the directory that gets the compiled code is <class-path>/<package-path>. If the destination directory exists already, the generated .class file goes into that directory. If it does not exist, the compiler automatically creates the required directory and then inserts the generated .class file into it. Thus, if you plan to use this option, you do not need to create the directory structure in Figure A4.2b explic- itly. You can let the compiler do it for you as you go along. Typical IDE’s also provide ways to do this.
If you are developing a Java application for use by others, you’ll probably want to organize your appli- cation’s classes into a package structure and store the .class files in a corresponding directory structure,
Apago PDF Enhancer
as previously described. When you have finished developing your application, it’s straightforward to use a program like WinZip to compress all your application’s files into a single file, perhaps called IPWJ.zip. After downloading this .zip file, your customer can insert this .zip file anywhere in his or her directory structure and then establish a class path to that .zip file which includes the name of the .zip file itself as the final element in the class path. For example, if the IPWJ.zip file is in your customer’s C:/myJava directory, your customer’s CLASSPATH should include this class path:
C:/myJava/IPWJ.zip
This enables your customer’s Java compiler to access all of your package classes while they are still in their compressed form. Notice that the class path to a .zip file should include the .zip file itself, but if you unzip the file, the class path should go only to the containing directory.
Of course, this also works the other way. If you acquire a Java application developed by someone else, it will probably have its classes pre-packaged and compressed into a .zip file (or some other type of archive). In that case, you may be able to put that compressed .zip file wherever you want in your computer’s direc- tory structure, and then just add a class path to that .zip file, with the .zip filename as the final element in that class path.