简体   繁体   English

错误:找不到或加载主类应用程序

[英]Error: Could not find or load main class Application

Could any one show me how do I run the main method on this code, please? 有人可以告诉我如何在此代码上运行main方法吗? I would like to check if there is any compile time error or run-time error, but I got this error message "Could not find or load main class Application". 我想检查是否存在任何编译时错误或运行时错误,但我收到此错误消息“找不到或加载主类应用程序”。

class Book {
private static int internalID = 0;
private String isbn;
private int myID;

public Book(String isbnP) {
    if (isbnP == null) {
        throw new IllegalArgumentException("null ISBN not accepted");
    }
    isbn = isbnP;
    myID = internalID++;
}

public String getBookinfo() {
    return isbn;
}

public String toString() {
    return "<" + myID + "," + isbn + ">";
}
// To Do: Override Object.equals()
// Two objects are equal iff isbn of the two books are same

}

class ComSBook extends Book {
private String category;

public ComSBook(String isbnP, String catP) {
    super(isbnP);
    category = catP;
}

@override
public String getBookInfo() {
    return "ComS " + category + " " + super.getBookinfo();
}
}

class NetworkBook extends ComSBook {
private boolean isWithCD;

public NetworkBook (String isbnP, boolean withCD){
    super(isbnP,"Network");
    isWithCD = withCD;
}
@override
public String getBookInfo(){
    return super.getBookInfo() + " withCD: " + isWithCD;
}
}


class ReviewPolymorp{
public static void main(String[] args){
    Book abook = new Book("A-1");
    Book bbook = new Book("B-1");
    ComSBook csbook = new ComSBook("C-11", "General");
    NetworkBook netbook = new NetworkBook("N-11", true);

    System.out.println(abook);
    System.out.println(bbook);
    System.out.println(csbook);
    System.out.println(netbook);

    abook = csbook;
    System.out.println(abook.getBookinfo());

    bbook = netbook;
    System.out.println(bbook.getBookinfo());

    netbook = (NetworkBook) bbook;
    System.out.println(netbook.getBookinfo());

    netbook = (NetworkBook) csbook;
    System.out.println(netbook.getBookinfo());

    netbook = csbook;

}
}

You're file should be ReviewPolymorp.java and class ReviewPolymorp{ should be public , since it the lauching point of the program with the main method 您的文件应该是ReviewPolymorp.javaclass ReviewPolymorp{应该是public ,因为它是使用main方法的程序的启动点

You may also want to check that the Main class in the project is ReviewPolymorp . 您可能还需要检查项目中的Main类是否为ReviewPolymorp I only know how to do this in Netbeans 我只知道如何在Netbeans中做到这一点

  • Right-click on the Project 右键单击项目
  • Select Properties 选择属性
  • Click Run 点击运行
  • Make sure your ReviewPolymorp i the Main Class with fully qualified name eg mypackage.ReviewPolymorp 确保您的ReviewPolymorp i在主类中具有完全限定的名称,例如mypackage.ReviewPolymorp
  • Then rebuild your project 然后重建您的项目

When I copy your code to my IDE(It's eclipse),a compile error show up on this line: 当我将代码复制到IDE(eclipse)时,此行显示编译错误:

netbook = csbook;

You can't cast an instance of super class to subclass. 您不能将超类的实例转换为子类。

After disable this line,I get output from console once run the code: 禁用此行后,运行代码后,我将从控制台获得输出:

<0,A-1>
<1,B-1>
Exception in thread "main" <2,C-11>
<3,N-11>
C-11
N-11
N-11
java.lang.ClassCastException: com.test.ComSBook cannot be cast to com.test.NetworkBook
at com.test.ReviewPolymorp.main(ReviewPolymorp.java:75)

It's the same problem with the compile error above. 上面的编译错误也是同样的问题。

After disable these three lines 禁用这三行之后

netbook = (NetworkBook) csbook;
System.out.println(netbook.getBookinfo());
csbook = netbook;

Code is work well without any modification. 代码无需任何修改即可正常运行。

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

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