【问题标题】:Error: Could not find or load main class [duplicate]错误:无法找到或加载主类 [重复]
【发布时间】:2012-11-02 03:53:57
【问题描述】:

我在运行以下代码时遇到问题,上面写着Error: Could not find or load main class,但我显然在代码中有一个主...

    package abc;

import java.io.IOException;
import javax.swing.JOptionPane;

// As its name implies, this is what is driving the program, asking for input, giving output, etc.
public class driver
{
    @SuppressWarnings("unused")
    public static void main(String args[]) throws IOException // a main that throws an exception... hmmm... yes it's odd, but apparently not illegal
    {
        ....}

    private static void processTransaction(String trans, Customer[] customers) throws IOException // this is the function that processes the given transaction
    {
        ...
}

// An object representing a person who may have one or more bank accounts
class Customer
{
    ...
}

class BankAccount 
{
    ...
}

class CheckingAccount extends BankAccount // extends BankAccount with added functionality
{
    ...
}

class SavingsAccount extends BankAccount // extends BankAccount with added functionality
{
    ...
}

【问题讨论】:

  • 你的 sscce 不是很短 :)
  • 我将其留在评论中而不是答案中,因为这是疯狂的猜测,但也许您的编译器正在寻找 main 类而不是 main 方法,或者可能是它寻找不会抛出 IOExceptionmain
  • 你不能有一个名为default的包,它不会编译。
  • @SamIam.. 我敢打赌,您没有将其发布为答案,从而为自己节省了很多声誉。
  • @user133466.. 你的编译单元的名称是什么?是driver吗? P.S,请以大写字母开头的班级命名。

标签: java main


【解决方案1】:

尝试将您的类访问说明符公开

public class driver

【讨论】:

  • 出现新错误:public type driver must be defined in its own file...
  • @user133466.. 你去。这就是问题所在。将您的文件名更改为driver.java
  • @RohitJain 工作!!!谢谢!!!
  • @user133466.. 好。现在你可以接受这个答案了。当然是 6 分钟后。
猜你喜欢
  • 2017-02-02
  • 2023-03-03
相关资源
最近更新 更多