Computer Science 15-100, Spring 2009
Class Notes: Getting Started With Packaging: Simple Console Programs
- Create a new project
- Add jconsole.jar to
the project path
- Create MyProgram.java
- Create MyApplication.java
- Create the executable jar
file
- Run the executable jar file
Getting Started with Packaging:
Simple Console Applications (and JConsole)
- Create a new project
In JCreator: File / New / Projects / Empty Project
- 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.
- 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!!!");
}
}
- 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();
}
}
- Create the executable jar
file
- Create "classes" directory
- Copy all necessary class files (MyProgram.class, MyApplication.class,
etc) into classes directory
- 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)
- Create manifest
- Must live in top-level of classes directory
- Delete any pre-existing manifest file!
- Case-sensitive, space-sensitive, and must end in a blank line!
- Main-Class should be the class where your "main" method resides
- Here is our
manifest.txt file (note that it ends with a blank line!)
Main-Class: MyApplication
- Create the jar file
- Run command shell and cd to classes directory
- 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 .
- Move the MyProgram.jar file to your deployment directory
- 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