Java - Create Packages

Creating packages is a great way to share and reuse code. Once you have developed/found lots of packages, you will find that you will be able to build complex applications rapidly by just glueing them together. You will need to write less code, thus have less code to maintain and debug.

    Create a set of useful classes that you want to turn into a package. I can't really help you with this one, but for this example, I am going to use the files shown below:
    Compile the source files using javac into class files.
    javac /path/to/source/*
    We need to know your "vendor" name. If you have a personal website, this would be your website's address in reverse, such as com.blogspot.programster, however, since I am not sharing this publicly, I am going to just use "programster".
    Think of a name for your package. E.g. for my first package, I am going to call it "core" because it forms my "core" libraries that I use in all projects.
    The vendor name and package name need to be all lowercase, not have any hyphens, but can have underscores.
    Place your
    *.class
    files, inside a folder that is named after the package, and place that folder within a folder named after the vendor. If your vendor name has
    .
    characters, such as for the reversed domain, create folders for each part of the name, not including the
    .
    . For example, my folder path (if I was using this domain for my vendor name) would be:
    com/blogspot/programster/core/[java source files here]
    Navigate to the folder that contains the top folder of the path you just created. and run the following command:
    jar -cf [package-name].jar [top folder name]
    For example:
    java -cf core.jar com
    Now you should have a file called [package name].jar in the folder that you just ran the command. This is your finished package that you can share with others and include in projects.

No comments:

Post a Comment