简体   繁体   English

Java - “错误:无法找到或加载主类”错误

[英]Java – “Error: Could not find or load main class” Error

the error and the class http://puu.sh/1ITnS.png 错误和类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. 当我将类文件命名为Main.class时,java表示它的名称错误,当我将其命名为shop.Main.class时,它表示无法找到主类。 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 取消

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

Execute these commands: 执行以下命令:

cd ..
java shop.Main

You can't run java code from inside a package you are trying to reference. 您无法从您尝试引用的包中运行Java代码。

保持Main.class并尝试java shop.Main来自java文件夹中的命令行

compile: ~/java> javac shop/Main.java 编译:〜/ java> javac shop / Main.java

run: ~/java> java shop.Main 运行:〜/ java> java shop.Main

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). 我建议使用IDE(Eclipse和Netbeans都是好的和免费的选择)。

Your example will work if you place Main.class in folder called "shop" and then from project root folder execute "java shop/Main" 如果将Main.class放在名为“shop”的文件夹中然后从项目根文件夹执行“java shop / Main”,那么您的示例将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM