Results 1 to 3 of 3
  1. #1
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default Help a newbie with packages please!

    Hello everyone! I have a quick question for you. Every day i set a java goal for myself of something i really want to learn, and im usually very good at acomplishing my goal considering java is extremely....documented. Im running into a bit of a brick wall with packages though.
    Ive found many resources and 'package tutorials' but i still cant quite get it

    What im TRYING to do is simply to make my own package that i can import into any
    of my other java programs

    I created a folder called 'PACKS' in my home folder
    Id like this folder to contain classes ive programmed which i can import when i need them
    As a very simple example, i want to make a folder(package) called: printUtils
    in my packs folder, which contains classes i can import and use for printing

    i created the packs/printUtils folder, and inside i made a pLine.java file with this code
    package printUtils;
    public class pLine {
    void printALine() {
    System.out.println("This is a sample line");
    }
    }

    and compiled it. Now i have a packs/printUtils/pLine.class file which is part of the
    printUtils package

    Now if i wanted to run this class (if it had a main method) i know id have to run it from the packs directory like this: java printUtils.pLine


    BUT what if i wanted to IMPORT it into another program i was writing in my /home/javasrc/proj/mynewproject folder

    import printUtils.pLine;
    seems like the right way to go about it, but javas not going to know what printUtils is from my proj directory. Do i need to change my classpath on my machine to include my packs folder? and if thats the answer, what do i need to do to my program to make it portable? make a jar out of it and put any class i inport in a packs folder IN that jar?



    Thanks for the help in advance! :D

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    seems like the right way to go about it, but javas not going to know what printUtils is from my proj directory. Do i need to change my classpath on my machine to include my packs folder?

    Yes - you change the classpath so that it includes all of the packages you might use. Typically you do this when you invoke the Java runtime or compiler, or with some settings in an IDE.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The section in Oracle's Tutorial on Packages is good.

    (However I would not recommend what they suggest about using the classpath system variable to set the classpath. It is better done from the command line or by the IDE once per execution of java or javac.)

Similar Threads

  1. packages
    By fogus in forum New To Java
    Replies: 1
    Last Post: 03-24-2009, 06:14 AM
  2. Replies: 3
    Last Post: 02-12-2008, 11:03 AM
  3. Using packages
    By prfalco in forum New To Java
    Replies: 5
    Last Post: 01-31-2008, 10:38 PM
  4. packages
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 12:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •