Creating a Class Library JAR from the Command Line

Putting class libraries in JAR files allows for code reuse, which is one of the fundamental principles of software engineering. If the code sucks, though, reusing it might be a bad thing. Hm, I just thought of that.

1. In any convenient location, create a new directory named “ClassLibraryTest”.

2. Copy the file JavaPathAndProgramNameSet.bat to the newly created ClassLibraryTest directory. This file was created in a previous tutorial.

3. Still in the ClassLibraryTest directory, create a text file named “ClassLibraryTest.java”, containing the following text.

import BookLibrary.*;

public class ClassLibraryTest
{
    public static void main(String[] args)
    {
        Author hemingway = new Author("Ernest", "Hemingway");

        Book book = new Book
        (
            "The Old Man and the Sea",
            hemingway,
            1952
        );

        String citation = book.buildCitationString();

        System.out.println(citation);
    }
}

4. Still in the ClassLibraryTest directory, create a new subdirectory named “BookLibrary”.

5. Copy the JavaPathAndProgramNameSet.bat file to the newly created BookLibrary directory.

6. In the BookLibrary directory, create a new text file named “Book.java”, containing the following text.

package BookLibrary;

public class Book
{
    public String name;
    public Author author;
    public int yearPublished;

    public Book(String name, Author author, int yearPublished)
    {
        this.name = name;
        this.author = author;
        this.yearPublished = yearPublished;
    }

    public String buildCitationString()
    {
        String returnString = "";

        returnString += author.lastNameFirstName() + ". ";
        returnString += "(" + this.yearPublished + "). ";
        returnString += "_" + this.name + "_.";

        return returnString;
    }
}

7. Still in the BookLibrary directory, create a new text filed named “Author.java”, containing the following text.

package BookLibrary;

public class Author
{
    public String firstName;
    public String lastName;

    public Author(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String lastNameFirstName()
    {
        return this.lastName + ", " + this.firstName;
    }
}

8. Still in the BookLibrary directory, create a new text file named “PackageBuildAndJarify.bat”, containing the following text.

call JavaPathAndProgramNameSet.bat
cd ..

for %%j in (%programName%\*.java) do echo %%j >>%programName%\_SourceFileNames.txt
%javaPath%\javac.exe @%programName%\_SourceFileNames.txt 

for %%j in (%programName%\*.class) do echo %%j >>%programName%\_ClassFileNames.txt
%javaPath%\jar.exe cvf %programName%.jar @%programName%\_ClassFileNames.txt

cd %programName%

pause

9. Double-click the icon PackageBuildAndJarify.bat to run it. A console window will appear, and the source files in the BookLibrary directory will be compiled to class files, which will in turn be packaged in a jar file.

10. Navigate back up to the ClassLibraryTest directory.

11. In the ClassLibraryTest directory, the file “ClassLibraryTest.jar” should now be present.

12. In the ClassLibraryTest directory, create a new text file named “PackagesToIncludeSet.bat”, containing the following text.

set packagesToIncludeDelimitedWithSemicolons=BookLibrary.jar
set packagesToIncludeDelimitedWithSpaces=BookLibrary.jar

13. Still in the ClassLibraryTest directory, create a new text file named “ProgramBuildJarifyRun.bat”, containing the following text.

call JavaPathAndProgramNameSet.bat
call PackagesToIncludeSet.bat

%javaPath%\javac.exe -cp %packagesToIncludeDelimitedWithSemicolons% %programName%.java

dir /b *.class >_ClassFileNames.txt
echo Main-Class: %programName%>%programName%.mf
echo Class-Path: %packagesToIncludeDelimitedWithSpaces%>>%programName%.mf
%javaPath%\jar.exe cvfm %programName%.jar %programName%.mf @"_ClassFileNames.txt"

%javaPath%\java.exe -jar %programName%.jar

pause

14. Double-click the icon for “ProgramBuildJarifyRun.bat” to run it. A console window will appear, and the ClassLibrary.java file will be compiled to class files, which will in turn be packaged in a jar file. This jar file will then be executed, and the text “Hemingway, Ernest. (1952). _The Old Man and the Sea_.” should appear somewhere on the screen.

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s