JMU
Lab: Skills - The JAR Utility


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Review: When you compile and execute applications written in Java, the byte code for all of the necessary classes must be available. This part of the lab will help you review what happens when this is not the case.
  1. Review the file JMUview.java and make sure you understand it.
  2. Compile the file JMUview.java.
  3. What errors were generated?
  4. Why were these errors generated?
  5. Download the files TextEditor.class and TextWindow.class into the directory for this lab.
  6. What are these files (i.e., what are .class files)?
  7. Compile the file JMUview.java.
  8. What happened?
  9. Delete the files TextEditor.class and TextWindow.class.
  10. Execute JMUview.
  11. What error was generated?
  12. Why was this error generated?
  13. Download the files TextEditor.class and TextWindow.class into the directory for this lab.
  14. Execute JMUview to make sure it works.
2. Creating and Using .jar Files: Java applications often consist of a large number of classes. Since each class is in its own file, this means that Java applications often consist of a large number of files. The Java Archive (JAR) utility can be used to combine files in one or more .jar files. This part of the lab will help you learn how to create and use .jar files.
  1. Enter the following command (in a teminal/command window):
    jar -cvf text.jar TextEditor.class TextWindow.class
  2. What file was created?
  3. Delete TextEditor.class and TextWindow.class.
  4. Delete JMUview.class.
  5. Compile JMUview.java.
  6. Why didn't it compile?
  7. Enter the following command (in a teminal/command window):
    javac -cp text.jar JMUview.java
  8. What does the -cp option do?
  9. Execute JMUview.
  10. Why didn't it execute?
  11. If you are using Unix/OS-X, enter the following command (in a teminal/command window):
    java -cp text.jar:. JMUview

    If you are using MS-Windows, enter the following command (in a teminal/command window):
    java -cp text.jar;. JMUview

  12. What does the : (in Unix/OS-X) and/or ; (in MS-Windows) do in the classpath?
  13. What does the . do in the classpath?
  14. If you use an IDE to compile and execute, set the classpath appropriately and compile JMUview.java. (For example, in jGRASP you can use [Settings]+[PATH/CLASSPATH] and choose either workspace or project. Then, you can create a new classpath entry by clicking on [New] and browsing to the .jar file.)
3. Creating and Using Executable .jar Files: Entire applications, including the "main" class, can be placed in a .jar files. This part of the lab will help you learn how to create and use executable .jar files.
  1. Delete all .class files and text.jar.
  2. Download the files TextEditor.class and TextWindow.class into the directory for this lab.
  3. Compile JMUview.java.
  4. Create a file named JMUview.mft (called a manifest) that contains the following:

    Manifest-Version: 1.0
    Main-Class: JMUview
    Class-path: .
        

  5. Enter the following command (in a teminal/command window):
    jar -cvfm JMUview.jar JMUview.mft *.class
  6. What does the -m option do?
  7. What file was created?
  8. Delete all of the .class files.
  9. Enter the following command (in a teminal/command window):
    java -jar JMUview.jar
  10. How do you know that the code in JMUview.jar (and not some other version) was executed?
  11. Depending on how it is setup, your operating system may recognize executable .jar files. Try clicking/double-clicking on JMUview.jar to see what happens.
4. More on Creating Executable .jar Files: This part of the labe will help you make sure that you really understand what's going on.
  1. Create an executable .jar file for an application you developed earlier in the semester (either a lab or a programming assignment).
  2. What did you do? (Note: Keep these step-by-step instructions. You'll almost certainly want to refer to them in the future.)

Copyright 2011