Prog 2 Classpath Updated 

Packages, classpath and .jar files

What is the content of JDK ?

JDK contains of: Note the difference between javac and java - dont mix them!

Choice of editor

JDK offers no editor. You will have to use your favorite-editor like:. Remember to save file with the extension .java. Default extension in the above mentioned editors are
.txt,.doc and .java..
The Java-compiler (javac) can only be used on files with the extension.java.

Adding to PATH

Most of the following will be automatically set up by JDK and through your editor.
The programs javac and java can be called directly from the DOS-prompt.
But before that the diredtory of these programs must be added to the PATH.
This is done by adding a line to the file c:\autoexec.bat:
  1. Open c:\autoexec.bat in n editor e.g. notepad.
  2. Add the line SET PATH=%PATH%;C:\JDK1.4.2\BIN
  3. Save the file and leave notepad.
  4. Restart the pc (remember to close open programs before)
  5. Write "set" in the DOS-prompt and verify thet the PATH contains JDK1.4.2\BIN.
Every time a program is initiated from the  DOS-prompt,  Windows will search in the directories given in the PATH, until the file is found. If the fiel is not found in one of the directories in the PATH, it cannot be run from the  DOS-prompt [butr the file can easily exist in another directory - as not all directories are given in the PATH].

The programs javac and java are are normally posioned in the directory C:\JDK1.4.2\BIN, when using standard installation. If JDK is installed in another directory or drive you must use this position instead of the above mentioned in autoexec.bat.

What is a CLASSPATH?

Internally Java use a socalled CLASSPATH to find other classes in other directories.
CLASSPATH can be changed in the same way as PATH can be changed.
It is advantageous to position user defined  packages (those you do yourself)for a specific system component as sub directories to a certain directory.
The path to this directory is to be included in the CLASSPATH, meaning that the respective packages can be found by the  compilere and used by the user program. If you often get error messages like "class not found in import" you should do the below mentioned modifications.

Example Windows 9X: Antag at du har lavet 3 packages: pack1, pack2 og pack3 alle anbragt i kataloget mypackages. For at få adgang til  klasserne i de tre packages tilføjes flg. linie sidst i filen autoexec.bat:

    SET CLASSPATH=%CLASSPATH%;C:\homedirectory\mypackages

Now the Java-compiler will also search in mypackages for packages.

Example Windows 2000 (XP): Assume you have made 3 packages: pack1, pack2 and pack3 all positioned in the directory mypackages. To get access to the classes in these 3 packages choose:

    Start menu -> Control panel -> System -> Avanceret ->Miljøvariable
    Start -> Settings -> Control Panel -> System -> Advanced -> Environment Variables

Now a form pops up and it can be filled out with:
    Variable Name: classpath
    Variable Value: C:\homedirectory\mypackages
Thereafter the Java-compiler will also search in mypackages for packages and their content.

What is jar ?

Java Archive (jar) is Javas system for compressing (via zip) and collect .class files in an archive.
By adding the archive-path to the CLASSPATH the Java-compileren can find the  .class filerne.
Later the archive can be extended with more .class filer as well as  .class files can be removed from the archive.
jar has some options e.g.:
        c    create new archive
        v    generate verbose output on standard output
        f    specify archive file name
        u    update existing archive
For a complete dexcription se Sun's home pages or give the following command from the DOS-prompt:

     jar

Java's archive system really easy work of  systematizing. If e.g. an applet will have to use many files it is great just once and only once to transfer (and remember the name of) the archive file only.

Example: Assume that you have two .class files:  my1.class og my2.class and want to save them in the archive myclasses.jar. From the DOS-prompt give the following command:

    jar -cvf myclasses.jar my1.class my2.class

Move the archive to an appropriate directory and add the directory-path to the classpath.
Later the file mynew.class is added by the command:

  jar -uvf myclasses.jar mynew.class

As jar saves special Metadata of the classes you cannot just .zip the files.
A few editors (maybe Netbeans, but not TextPad) offers the jar faciliteten on projects.
 
 
 

Maintenance by claudius@rhs.dk