java.lang.NoClassDefFoundError


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: java.lang.NoClassDefFoundError

  1. #1
    Join Date
    Nov 2006
    Posts
    1

    java.lang.NoClassDefFoundError

    Having problems with my assignment for college. Newbie and new to this forum so bare with me.
    Been asked to do the following:
    "Write a class called, Product that has the following attributes a name, a price and description. Write get and set methods for each of the attributes. It should have a constructor that takes three parameters, name, price and description. It should also have a toString method that returns the details of the Product.A stock room can hold a fixed number of Products, e.g. fixed number is 10 then the maximum number of products a stock room can hold is 10.

    Create a class called, StockRoom that will store the fixed number of Products (hint: array). The StockRoom will have a constructor that takes an integer, which represents the fixed number (or maximum number) of Products that the stock room can hold.

    This class should include methods to allow for
    - new products to be added to the stock room cannot store more than maximum number allowed in the stock room.
    - a list of all products to be printed out.

    Write an application to test the methods of the StockRoom."


    This is what I have at the moment.
    -----------------------------------------------------------------------
    public class Product{

    public String name;
    public Double price;
    public String description;
    public String productString;

    public Product(){
    }


    public Product(String name, Double price, String description){
    this.name=name;
    this.price=price;
    this.description=description;
    }


    public String toString(){
    productString="name" + "price" + "description";
    return productString;
    }


    public void setName(String name){
    this.name=name;
    }

    public void setPrice(Double price){
    this.price=price;
    }

    public void setDescription(String description){
    this.description=description;
    }


    public String getName(){
    return name;
    }

    public Double getPrice(){
    return price;
    }

    public String geDescription(){
    return description;
    }



    }
    ------------------------------------------------------------------------
    public class StockRoom{

    public Product[] stockRoomArray;
    public int maxPoductsInStockRoom;
    public int productPosition;
    public Product productToAdd;


    public StockRoom(int maxPoductsInStockRoom){
    this.maxPoductsInStockRoom=maxPoductsInStockRoom;
    stockRoomArray = new Product[maxPoductsInStockRoom];
    }

    public void printProductList(){
    int n;
    for (n=0;n<maxPoductsInStockRoom;n++){
    System.out.println("Product in position "+n+": "+stockRoomArray[n]);
    }
    }

    public int addProduct(Product productToAdd){
    if (productPosition>=maxPoductsInStockRoom){
    System.out.println("The stock room is full!");
    }
    else{
    stockRoomArray[productPosition]= productToAdd;
    }
    return productPosition;
    }

    public void addPos(int productPosition, Product productToAdd){
    stockRoomArray[productPosition]= productToAdd;
    }

    }
    -----------------------------------------------------------------------
    When I run StockRoom I get java.lang.NoClassDefFoundError: StockRoom
    Both compiles without errors. Any idea whats wrong with the code? Any help is much appreciated.
    Email
    Last edited by corleone; 11-24-2006 at 06:42 AM.

  2. #2
    Join Date
    May 2005
    Posts
    22
    There is nothing wrong with your code if it compiles correctly. Occaisonally, Java gives this error. I have suffered the same error on various occaisions; usually it's because of the way you're trying to run the .class file.

    I know of no fix that works in every case, and can't offer any suggestions without knowing how you're trying to run StockRoom.class.

  3. #3
    Join Date
    Mar 2004
    Posts
    635
    How exactly did you plan on running a class with no main method? That error is being thrown because you're trying to run a class with no main method.

    Code:
    public static void main(String[] args)

  4. #4
    Join Date
    Dec 2006
    Location
    Kerala,India
    Posts
    51
    Try adding current directory (./) in your classpath and run.

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2007, 06:35 PM
  2. Replies: 2
    Last Post: 12-05-2005, 06:27 PM
  3. java.lang.NoClassDefFoundError ?
    By mahmood in forum Java
    Replies: 8
    Last Post: 10-20-2005, 07:42 AM
  4. java.lang.NoClassDefFoundError
    By srekcus in forum Java
    Replies: 2
    Last Post: 05-25-2005, 02:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center