3

the error and the class http://puu.sh/1ITnS.png

When I name the class file Main.class, java says it has the wrong name, and when I name it shop.Main.class it says that the main class can't be found. Can anyone help?

package shop;

import java.text.DecimalFormat;

public class Main
{  
    public static void main(String args[])
    {  
        Cart cart = new Cart(new Catalogue());
        printOrder(cart);
    }

    public static void printOrder(Cart cart)
    {
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println("Your order:");
        for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size(); 
            itemIndex++)
            if (cart.itemsInCart.products.get(itemIndex).quantity != 0)
                System.out.println(cart.itemsInCart.products.get(itemIndex).quantity 
                    + " " + cart.itemsInCart.products.get(itemIndex).name 
                    + " $"+ df.format(cart.itemsInCart.products.get(itemIndex).price) 
                    + " = $" + df.format
                    ((cart.itemsInCart.products.get(itemIndex).quantity 
                    * cart.itemsInCart.products.get(itemIndex).price)));

        double subtotal = 0;
        int taxPercent = 20;
        double tax;
        double total;

        for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size(); 
            itemIndex++)
            subtotal += cart.itemsInCart.products.get(itemIndex).quantity 
            * cart.itemsInCart.products.get(itemIndex).price;
        tax = subtotal * taxPercent / 100;
        total = subtotal + tax;


        System.out.print("Subtotal: $" + df.format(subtotal) 
            + " Tax @ " + taxPercent + "%: $" + df.format(tax) 
            + " Grand Total: $" + df.format(total));
    }  
}

Ignore between the following two lines

–––––––––––––––––––––––––

Edit Summary

Oops! Your edit couldn't be submitted because:

Your post does not have much context to explain the code sections; please explain your scenario more clearly.

cancel

––––––––––––––––––––––---

4
  • Java tutorial might help. Commented Jan 3, 2013 at 11:39
  • Either remove the package shop; line, or run java from the java folder Commented Jan 3, 2013 at 11:41
  • add the .class to your classpath Commented Jan 3, 2013 at 11:41
  • A classpath contains only directories and/or .jar files. Putting a .class file directly in the classpath is incorrect; it will always be ignored. Commented Jan 4, 2013 at 12:41

4 Answers 4

4

Execute these commands:

cd ..
java shop.Main

You can't run java code from inside a package you are trying to reference.

Sign up to request clarification or add additional context in comments.

Comments

2

keep it Main.class and try java shop.Main from command line in java folder

Comments

0

compile: ~/java> javac shop/Main.java

run: ~/java> java shop.Main

Comments

0

You should be careful to place classes in correct folders if compiling manually (package name equals folder name on disk). I recommend using an IDE (Eclipse and Netbeans are both good and free choices).

Your example will work if you place Main.class in folder called "shop" and then from project root folder execute "java shop/Main"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.