Results 1 to 11 of 11
  1. #1
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default Package Questions

    I am trying to complete a simple program that has two classes in a package and one class that runs them. I am working on this before school starts back, trying to get ahead. I am working out of a book called Teach yourself Java in 21 days.

    I have no problem compiling Item.java. I then take the item class file and save it to C:\dev\java\org\cadenhead\ecommerce( I have also entered this into the system CLASSPATH on my computer). When I try to compile the Storefront.java I get:

    C:\javaprograms\21Java\Day6>javac -classpath /dev/java/org/cadenhead/ecommerce/ Storefront.java

    Storefront.java:14: cannot find symbol
    symbol : class Item
    location: class org.cadenhead.ecommerce.Storefront
    public Item getItem(int i){
    ^
    Storefront.java:10: cannot find symbol
    symbol : class Item
    location: class org.cadenhead.ecommerce.Storefront
    Item it = new Item (id, name, price, quant);
    ^
    Storefront.java:10: cannot find symbol
    symbol : class Item
    location: class org.cadenhead.ecommerce.Storefront
    Item it = new Item (id, name, price, quant);
    ^
    Storefront.java:15: cannot find symbol
    symbol : class Item
    location: class org.cadenhead.ecommerce.Storefront
    return (Item)catalog.get(i);
    ^
    Note: Storefront.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    4 errors

    I also tried C:\javaprograms\21Java\Day6>javac Storefront.java

    Errors are the same.

    Item.java

    package org.cadenhead.ecommerce;

    import java.util.*;

    public class Item implements Comparable{
    private String id;
    private String name;
    private double retail;
    private int quantity;
    private double price;

    Item(String idIn, String nameIn, String retailIn, String quanIn){
    id = idIn;
    name = nameIn;
    retail = Double.parseDouble(retailIn);
    quantity = Integer.parseInt(quanIn);

    if (quantity > 400)
    price = retail * .5D;
    else if (quantity > 200)
    price = retail * .6D;
    else
    price = retail * .7D;
    price = Math.floor(price * 100 + .5) / 100;
    }
    public int compareTo(Object obj){
    Item temp = (Item)obj;
    if (this.price < temp.price)
    return 1;
    else if (this.price > temp.price)
    return -1;
    return 0;
    }

    public String getId(){
    return id;
    }

    public String getName(){
    return name;
    }

    public double getRetail(){
    return retail;
    }

    public int getQuantity(){
    return quantity;
    }

    public double getPrice(){
    return price;
    }
    }

    Storefront.java

    package org.cadenhead.ecommerce;

    import java.util.*;

    public class Storefront{
    private LinkedList catalog = new LinkedList();

    public void addItem(String id, String name, String price, String quant){

    Item it = new Item (id, name, price, quant);
    catalog.add(it);
    }

    public Item getItem(int i){
    return (Item)catalog.get(i);
    }

    public int getSize(){
    return catalog.size();
    }

    public void sort(){
    Collections.sort(catalog);
    }
    }

    GiftShop.java

    import org.cadenhead.ecommerce;

    public class GiftShop{
    public static void main(String[] args){
    Storefront store = new Storefront();
    store.addItem("C01", "MUG", "9.99", "150");
    store.addItem("C02", "LG MUG", "12.99", "82");
    store.addItem("C02", "MOUSEPAD", "10.99", "800");
    store.addItem("D01", "T-SHIRT", "16.99", "90");

    for (int i = 0; i < store.getSize(); i++){
    Item show = (Item)store.getItem(i);
    System.out.println("\nItem ID: " + show.getId() +
    "\nName: " + show.getName() +
    "\nRetail Price: " + show.getRetail() +
    "\nPrice: " + show.getPrice() +
    "\nQuantity: " + show.getQuantity());
    }
    }
    }

    Obviously GiftShop.java will not compile until it can find both the other class files.

    I sure could use some help on packages. I do not get what I am doing wrong.

    Kerry

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    First off, don't add the paths to your system CLASSPATH. That's not the way to do it for your app. Especially since you are defining the classpath in your call to javac (which means the system one will be ignored anyway).

    The problem is that the classpath you have defined in javac is incorrect. What you need is to tell it where the package sits (that is where the top level org directory is). You''ve gone too far, telling it where the class files themselves are.

    Try this - "javac -cp /dev/java Storefront.java".

    Because you have your stuff in packages, the class path needs to point to the package, and not the classes. And good-oh on the packages...the default package is the spawn of satan.
    :)

  3. #3
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    First off, don't add the paths to your system CLASSPATH. That's not the way to do it for your app. Especially since you are defining the classpath in your call to javac (which means the system one will be ignored anyway).

    The problem is that the classpath you have defined in javac is incorrect. What you need is to tell it where the package sits (that is where the top level org directory is). You''ve gone too far, telling it where the class files themselves are.

    Try this - "javac -cp /dev/java Storefront.java".

    Because you have your stuff in packages, the class path needs to point to the package, and not the classes. And good-oh on the packages...the default package is the spawn of satan.
    :)
    Thank you for the help. I changed the CLASSPATH in my system first to the root dirctory which is c:\dev\java.

    I then tried javac -cp /dev/java Storefront.java and I get:

    Note: Storefront.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.


    What does that mean? I have searched that on Google with not much success.

    Does that mean it did see Item.class when it compiled and now this is the only error left?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Do not use the system CLASSPATH for your libraries!
    Honestly...that's not the correct way to do it.

    As for the warning, it's because you are using LinkedList without saying what you are storing in the LinkedList. It's only a warning, but you should really be using LinkedList<Item>, since that's what you're storing. That also means you no longer need to cast on a get().

    ETA: But yes, it did compile.

  5. #5
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    I removed the CLASSPATH that I haded. I still have the C:\Program Files (x86)\Java\jre6\bin in there.

    I have the file and classes in the right locations, I believe, but still have issues. Your right, packages are horrible. But I still want to understand why.

    Directory of C:\dev\java\org\cadenhead\ecommerce

    08/10/2010 11:56 AM <DIR> .
    08/10/2010 11:56 AM <DIR> ..
    08/10/2010 11:32 AM 1,368 Item.class
    08/09/2010 11:35 PM 1,031 Item.java
    08/10/2010 11:40 AM 891 Storefront.class
    08/10/2010 11:32 AM 471 Storefront.java
    4 File(s) 3,761 bytes
    2 Dir(s) 359,780,913,152 bytes free

    C:\dev\java\org\cadenhead\ecommerce>cd\

    C:\>cd javaprograms\21Java\Day6

    C:\javaprograms\21Java\Day6>dir
    Volume in drive C has no label.
    Volume Serial Number is 3A16-5B4C

    Directory of C:\javaprograms\21Java\Day6

    08/10/2010 11:56 AM <DIR> .
    08/10/2010 11:56 AM <DIR> ..
    08/10/2010 12:14 PM 1,474 GiftShop.class
    08/10/2010 11:56 AM 664 GiftShop.java
    08/09/2010 01:10 PM 1,033 InstanceCounter.class
    08/09/2010 01:10 PM 554 InstanceCounter.java
    4 File(s) 3,725 bytes
    2 Dir(s) 359,780,913,152 bytes free

    C:\javaprograms\21Java\Day6>java GiftShop
    Exception in thread "main" java.lang.NoClassDefFoundError: org/cadenhead/ecommerce/Storefront
    at GiftShop.main(GiftShop.java:5)
    Caused by: java.lang.ClassNotFoundException: org.cadenhead.ecommerce.Storefront
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

    I really appreciate your help, maybe this book is not the one I should be studying, what do you think?

    Kerry

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Java is not finding the class Storefront because the class is in a package: org.cadenhead.ecommerce and that package is not on the classpath.
    Given this path: C:\dev\java\org\cadenhead\ecommerce
    Your java command should set the classpath to where the package path begins:

    java -cp C:\dev\java\;. GiftShop

    Note the ;. to include the current directory in the claspath

  7. #7
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Thanks for the schooling. That was a very interesting process for me. My first package. I have seen the packages get created when I used Netbeans, but I always deleted the package and just threw the files into one folder.

    So I understand now that the CLASSPATH is the java library, and if I create a package path that is in another folder that is outside the CLASSPATH, I have to use a command line argument to direct to the current path where the package is at.

    Thank you so much!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The classpath plus the package path concatenated together must point to the location of the class file.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    And I didn't say packages were horrible.
    I said the default package (ie "no package") is the spawn of satan.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Your right, packages are horrible
    It is one more level of complexity for the beginner to mess up. Save packages for the second level of learning java.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Beyond HelloWorld, I would argue that packages should be the standard (I was going to say "norm", but might have confused matters...;))

Similar Threads

  1. run package inside anthor package
    By AhmedAdel in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2010, 11:52 AM
  2. Replies: 7
    Last Post: 11-09-2009, 07:51 AM
  3. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  4. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM
  5. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 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
  •