Having homework issues again. This code compiles but when I try to execute it it gives me an "exception in thread main java.lang.nosuchmethoderror main"
Can anyone help me due tomorrow....Thanx

import java.awt.*;
   
      import java.awt.event.*;
   
      import java.text.NumberFormat; // used to format currency
   
      import javax.swing.*;
   
       
   
      import javax.swing.Icon;
   
      import javax.swing.ImageIcon;
   
      import javax.swing.JButton;
   
      import javax.swing.JFrame;
  
      import javax.swing.JLabel;
  
      import javax.swing.JPanel;
  
      import javax.swing.JScrollPane;

      import javax.swing.JTextArea;

     
  
      class InventoryMain implements ActionListener
  
      {
  
      CD[] product;
  
      JTextField[] fields;
  
      NumberFormat nf;
  
       
  
      public void actionPerformed(ActionEvent e)
  
      {
  
      int index = ((JComboBox)e.getSource()).getSelectedIndex();
  
      populateFields(index);
  
      }
  
       
  
      public static void main(String args[])
  
      {
  
      try
  
      {
  
      UIManager.setLookAndFeel(
        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  
      }
  
      catch (Exception e)
  
      {
  
      System.err.println(e.getClass().getName() + ": " + e.getMessage());
  
      }
  
      InventoryMain test = new InventoryMain();
  
      test.initCD();
  
      test.showGUI();
  
      test.populateFields(0);
  
      }
  
       
  
      /**
  
        *
  
        */
  
      public void initCD() {
  
      //Create an array
  
      product = new CD[10];

       
  
      //fill in the classes
  
      product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
  
      product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
  
      product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
  
      product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
 
      product[4] = new CD( 5, "Down", 24.00, 10, "00789");
  
      product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
  
      product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
  
      product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
  
      product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
        product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");
  
       
  
      //Sort elements in array in alphabetical order by product name
  
      // product[0].sortItems(product);
  
      final JButton firstBtn = new JButton("First"); // first button
  
      final JButton prevBtn = new JButton("Previous"); // previous button
  
      final JButton nextBtn = new JButton("Next"); // next button
  
      final JButton lastBtn = new JButton("Last"); // last button
  
      final JLabel label; // logo
  
      final JTextArea textArea; // text area for product list
  
      final JPanel buttonJPanel; // panel to hold buttons
  
      //JLabel constructor for logo
  
      Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
  
      label = new JLabel(logo); // create logo label
  
      label.setToolTipText("Company Logo"); // create tooltip
  
       
  
       
  
      }
  
       
  
       
  
       
  
      private void showGUI() {
  
      JLabel l;
  
      JButton b;
  
      fields = new JTextField[9];
  
      JComboBox combo = new JComboBox();
  
      for(int j = 0; j < product.length; j++)
  
      combo.addItem(product[j].getName());
  
      combo.addActionListener(this);
  
      JFrame f = new JFrame("InventoryGUI");
  
      Container cp = f.getContentPane();
  
      cp.setLayout(new GridBagLayout());
  
      cp.setBackground(UIManager.getColor("control"));
  
      GridBagConstraints c = new GridBagConstraints();
  
      c.gridx = 0;
  
      c.gridy = GridBagConstraints.RELATIVE;
  
      c.gridwidth = 1;
  
      c.gridheight = 1;
  
      c.insets = new Insets(2, 2, 2, 2);
 
      c.anchor = GridBagConstraints.EAST;
 
      cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a');
 
      cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b');
 
      cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c');
 
      cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d');
 
      cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e');
 
      cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
 
      SwingConstants.CENTER), c); l.setDisplayedMnemonic('f');
 
      cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g');
 
      cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h');
 
      cp.add(combo, c);
 
      c.gridx = 1;
 
      c.gridy = 0;
 
      c.weightx = 1.0;
 
      c.fill = GridBagConstraints.HORIZONTAL;
 
      c.anchor = GridBagConstraints.CENTER;
 
      cp.add(fields[0] = new JTextField(), c);
 
      fields[0].setFocusAccelerator('a');
 
      c.gridx = 1;
 
      c.gridy = GridBagConstraints.RELATIVE;
 
      cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b');
 
      cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c');
 
      cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d');
 
      cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e');
 
      cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f');
 
      cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g');
 
      cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h');
 
      cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i');
 
      c.weightx = 0.0;
 
      c.fill = GridBagConstraints.NONE;
 
      cp.add(b = new JButton("OK"), c);
 
      cp.add(b = new JButton("First"),c); // set up panel
 
      cp.add(b = new JButton("Prev"),c);
 
      cp.add(b = new JButton("Next"),c);
 
      cp.add(b = new JButton("Last"),c);
 
      b.setMnemonic('o');
 
      f.pack();
 
      f.addWindowListener(new WindowAdapter()
 
       
 
      {
 
      public void windowClosing(WindowEvent evt)
 
      {
 
      System.exit(0);
 
      }
 
      });
 
      f.setVisible(true);
 
       
 
      }
 
       
 
      private void populateFields(int index) {
 
      CD cd = product[index];
 
      fields[0].setText(Long.toString(cd.getNumberCode()));
 
      fields[1].setText(cd.getName());
 
      fields[2].setText(Long.toString(cd.getUnits()));
 
      fields[3].setText(Double.toString(cd.getPrice()));
 
      fields[4].setText(Double.toString(cd.getSum()));
 
      fields[5].setText(Double.toString(cd.totalAllInventory(product)));
 
      fields[6].setText(cd.getCode());
 
      fields[7].setText(Double.toString(cd.getSum()*.05));
 
      }
 
      }
 
       
 
      class CD {
 
       
 
      int itemNumber;
 
      String name;
 
      int units;
 
      double price;
 
      String itemCode;
 
       
 
      public CD(int n, String name, double price, int units, String itemCo) {
 
      itemNumber = n;
 
      this.name = name;
 
      this.price = price;
 
      this.units = units;
 
      itemCode = itemCo;
 
      }
        
 
      public int getNumberCode() { return itemNumber; }
 
      public String getName() { return name; }
 
      public int getUnits() { return units; }
 
      public double getPrice() { return price; }

      public double getSum() { return units*price; }

      public String getCode() { return itemCode; }
 
       
 
      public double totalAllInventory(CD[] cds) {
 
      double total = 0;
 
      for(int j = 0; j < cds.length; j++)
 
      total += cds[j].getSum();
 
      return total;
 
      }
 
      }
 
       
 
      public class Inventory5 {
 
       
 
      String productnumber;
 
       
 
      String name;
 
       
 
      int numberofunits;
 
       
 
      double priceperunit;
 
       
 
      String itemcode;
 
       
 
       
 
      // Create a new instance of Inventory
 
      // main constructor for the class
 
      public Inventory5(String Item_Number, String Item_Name, int Items_in_Stock,
 
      double Item_Price, String Item_Code) {
 
      productnumber = Item_Number;
 
      name = Item_Name;
 
      numberofunits = Items_in_Stock;
 
      priceperunit = Item_Price;
 
      itemcode = Item_Code;
 
      }
 
       
 
      public void setItemName(String Item_Name) {
 
      // sets the items name
 
       
 
      name = Item_Name;
 
      }
 
       
 
      public void setItemCode(String Item_Code) {
 
      // sets the items name
 
       
 
      itemcode = Item_Code;
 
      }
 
      public void setItemNumber(String Item_Number) { // Sets the Product =Number
 
      // for the item
 
       
 
      productnumber = Item_Number;
 
      }
 
       
 
      public void setItemsInStock(int Items_in_Stock) { // sets the =number of
 
      // units in stock
 
       
 
      numberofunits = Items_in_Stock;
 
      }
 
       
 
      public void setItemPrice(double Item_Price) { // sets the price of =the
 
      // item
 
      priceperunit = Item_Price;
 
      }
 
       
 
      public String getItemName() { // returns the Product Name of this item
 
      return name;
 
      }
 
       
 
      public String getItemCode() { // returns the Product Name of this item
 
      return itemcode;
 
      }
 
       
 
      public String getItemNumber() { // returns the Product Number of the =item
 
       
 
      return productnumber;
 
      }
 
       
 
      public int getItemsInStock() { // returns how many units are in stock
 
      return numberofunits;
 
      }
 
       
 
      public double getItemPrice() { // returns the price of the item
 
      return priceperunit;
 
      }
 
       
 
      public double getInventoryIValue() { // returns the total value of =the stock
 
      // for this item
 
      return priceperunit * numberofunits;
 
      }
 
       
 
      public static double getTotalValueOfAllInventory(Inventory5 [] inv) {
 
       
 
      double tot = 0.0;
 
       
 
      for(int i = 0; i < inv.length; i++)
 
       
 
      tot += inv[i].getInventoryIValue();
 
       
 
      return tot;
 
       
 
      }
 
       
 
      public static Inventory5[] sort(Inventory5 [] inventory)
 
      {
 
      Inventory5 temp[] = new Inventory5[1];
 
       
 
      //sorting the array using Bubble Sort
 
      for(int j = 0; j < inventory.length - 1; j++)
 
      {
 
       
 
      for(int k = 0; k < inventory.length - 1; k++)
 
      {
 
       
 
      if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
 
      {
 
       
 
      temp[0] = inventory[k];
 
      inventory[k] = inventory[k+1];
 
      inventory[k+1] = temp[0];
 
       
 
      }//end if
 
       
 
      }//end for loop
 
       
 
      }//end for loop
 
       
 
       
 
      return inventory;
 
      }
 
       
 
      public String toString()
 
      {
 
      StringBuffer sb = new StringBuffer();
 
       
 
       
 
      sb.append("CD Title: \t").append(name).append("\n");
 
      sb.append("Item Code: \t").append(itemcode).append("\n");
 
      sb.append("Item #: \t").append(productnumber).append("\n");
 
      sb.append("Number in stock:\t").append(numberofunits).append("\n");
 
      sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
 
      sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryIValue()));
 
       
 
      return sb.toString();
 
      }
 
       
 
       
 
      }

when I try to execute it it gives me an "exception in thread main java.lang.nosuchmethoderror main"

How are you trying to execute the program? Can you show the command line and the error?
Please copy the full text of the error message vs typing it in.

Edited 6 Years Ago by NormR1: n/a

How are you trying to execute the program? Can you show the command line and the error?
Please copy the full text of the error message vs typing it in.

I have attached a .doc file with a screen shot.
I was attempting to execute this in a command window.

Thanx
Jim

Can you copy and paste it here?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jim>cd\program files\java

C:\Program Files\Java>javac Inventory5.java

C:\Program Files\Java>java Inventory5
Exception in thread "main" java.lang.NoSuchMethodError: main

C:\Program Files\Java>javac Inventory5.java

C:\Program Files\Java>java Inventory5
Exception in thread "main" java.lang.NoSuchMethodError: main

C:\Program Files\Java>javac Inventory5.java

C:\Program Files\Java>java Inventory5
Exception in thread "main" java.lang.NoSuchMethodError: main

C:\Program Files\Java>java testmain
Exception in thread "main" java.lang.NoClassDefFoundError: testmain
Caused by: java.lang.ClassNotFoundException: testmain
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)
Could not find the main class: testmain. Program will exit.

C:\Program Files\Java>java ver
Exception in thread "main" java.lang.NoClassDefFoundError: ver
Caused by: java.lang.ClassNotFoundException: ver
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)
Could not find the main class: ver. Program will exit.

C:\Program Files\Java>


thanx again

jim

C:\Program Files\Java>java Inventory5
Exception in thread "main" java.lang.NoSuchMethodError: main

Does the class Inventory5 have the proper main() method?

The Java command looks in the class it is given for a main() method. It gives the error message if it does not find one.

In your posted code, the class: InventoryMain does have a main() method.

Have you tried: java InventoryMain

Edited 6 Years Ago by NormR1: n/a

I am still new to writing Java and having issues in this class.
The statement of java InventoryMain is to execute the program correct?

I changed the file name to InventoryMain and ran the javac and the tried to execute it and got same error message.

NetBeans said I should change the class to InventoryI and did so with the same result and even changed the file name to same an same result.
I have to go to work right now. and will be back in at 5:45 am EST and will check on your responce.

Thank you
I really need to change the product sold to office equipment from CD's.

The filename must be the same as the public class name.
The file InventoryMain must contain:
public class InventoryMain {

Renaming them won't work. The two names must match.

This article has been dead for over six months. Start a new discussion instead.