Sunday, November 13, 2005

how to create a jar file and how to use a jar file

Nowhere on the web could I find this. Therefore I thot should document this.

Suppose you want to create a package' mypackages.mypackage1' that will have one class 'MyClass'.

Step 1. Create a directory structure that matches the package hierarchy.
Suppose you are in c:\myjava
then make a hierarchy like c:\myjava\mypackages\mypackage1

Step 2. Create the java file and write the source for MyClass.
You can save it anywhere.
Be sure to have to the package info in the source file "mypackages.mypackage1.MyClass"

Step 3. Complile it and save the class file in c:\myjava\mypackages\mypackage1

Step 4. create jar
goto c:\myjava and create the jar "jar cvf toyoda.jar mypackages"

Step 5. using the jar
ensure that the jar file is in the classpath
like in windows do this: "set classpath=%classpath%;.\toyoda.jar"

when using this import the files "import mypackages.mypackage1.*;"

Step 6. Enjoy