Computer Science 15-100, Spring 2009
Class Notes:  Getting Started With Packaging:  Simple Console Programs


  1. Create a new project
  2. Add jconsole.jar to the project path
  3. Create MyProgram.java
  4. Create MyApplication.java
  5. Create the executable jar file
  6. Run the executable jar file

Getting Started with Packaging:  Simple Console Applications (and JConsole)

  1. Create a new project

    In JCreator:  File / New / Projects / Empty Project
     
  2. Add jconsole.jar to the project path

    Here is jconsole.jar
    In JCreator:  Project / Project Settings / Required Libraries / New (then add jconsole.jar archive, then select)
    Note:  we are using a slightly-modified version of BeanShell's JConsole class.  Here is our modified JConsole source code.
     
  3. Create MyProgram.java
    // MyProgram.java
    // A very simple console program to demonstrate packaging for deployment.
    
    import java.util.Scanner;
    
    public class MyProgram {
      public static void main(String[] args) {
        System.out.println("This is a simple console program to demonstrate");
        System.out.println("packaging for deployment.");
        System.out.println();
        System.out.println("By 'simple', we mean that this program does not use");
        System.out.println("resources (like jpeg's), does not read/write files,");
        System.out.println("nor anything else that would require more advanced");
        System.out.println("packaging/deployment techniques");
        System.out.println();
        Scanner scanner = new Scanner(System.in);
        while (true) {
          System.out.print("Enter an integer (or 'q' to quit): ");
          if (!scanner.hasNextInt()) {
            if (scanner.next().toLowerCase().startsWith("q"))
              break;
            else
              System.out.println("Please follow directions!");
          }
          else {
            int n = scanner.nextInt();
            System.out.println((n % 2 == 0) ? "EVEN!" : "ODD!");
          }
        }
        System.out.println("Bye!!!");
      }
    }
  4. Create MyApplication.java
    // MyApplication.java
    // Packaged "MyProgram" as a JConsole application.
    
    public class MyApplication {
      public static void main(String[] args) {
        JConsole.launch("MyProgram as an application", 750, 500); // width, height
        MyProgram.main(args);
        JConsole.exit();
      }
    }
  5. Create the executable jar file
     
    1. Create "classes" directory
       
    2. Copy all necessary class files (MyProgram.class, MyApplication.class, etc) into classes directory
       
    3. Extract all files from jconsole.jar into classes directory
      Easiest way:  copy jconsole.jar to jconsole.zip, then unzip
      Don't worry about extra files in jconsole.zip (they'll do no harm)
       
    4. Create manifest
      1. Must live in top-level of classes directory
      2. Delete any pre-existing manifest file!
      3. Case-sensitive, space-sensitive, and must end in a blank line!
      4. Main-Class should be the class where your "main" method resides
      5. Here is our manifest.txt file (note that it ends with a blank line!)
        Main-Class: MyApplication


         

    5. Create the jar file
      1. Run command shell and cd to classes directory
      2. Run this command:
           jar cvfm MyProgram.jar manifest.txt .
        (Don't forget that trailing dot!)
        You may have to specify the full path to your java installation, something like this:
           "C:\program files\Java\jdk1.5.0_12\bin\jar"  cvfm MyProgram.jar manifest.txt .
      3. Move the MyProgram.jar file to your deployment directory
         
  6. Run the Executable Jar File
    Here is the jar file we produced:  MyProgram.jar

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem