首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chain_of_Responsibility :线程"main“java.lang.NoClassDefFoundError中出现异常

Chain_of_Responsibility :线程"main“java.lang.NoClassDefFoundError中出现异常
EN

Stack Overflow用户
提问于 2012-11-06 22:27:33
回答 2查看 1.7K关注 0票数 1

我的问题如下:我有一个java代码,写在一个.java文件中-- Chain_of_Responsibility,代码在我的问题的最后。我在Linux上用

代码语言:javascript
复制
javac Chain_of_Responsibility.java

并将我所有的.class文件放在同一个目录中。然后我试着用我的程序运行

代码语言:javascript
复制
java Chain_of_Responsibility

并获取“线程中的异常"main”java.lang.NoClassDefFoundError: Chain_of_Responsibility“。我尝试使我的主函数成为静态函数,在不同的.java文件中编写我所有的类,但没有成功。所以我不知道该怎么做。你能帮帮我吗?

代码语言:javascript
复制
package COR;

public class Chain_of_Responsibility
{
public void main(String[] args)
{
    //Create the Chain of Responsibility

    Handler chain = build_chain();

    //Trying to handle the request (valid are cheese, meet or banana)

    chain.handle_request(args[1]);
}

private Handler build_chain()
{
    //Creating the chain

    Handler monkey = new Monkey();
    Handler wolve = new Wolve();
    Handler mouse = new Mouse();

    //First nide is Monkey, then Wolve and then Mouse

    monkey.set_next_handler(wolve);
    wolve.set_next_handler(mouse);

    //Returning the first node in the chain

    return monkey;
}
}

abstract class Handler
{
Handler next_handler;

public void set_next_handler(Handler next_handler)  
{
    //Setting next handler in the chain

    this.next_handler = next_handler;
}

public abstract void handle_request(String request);
}

class Mouse extends Handler
{
public void handle_request(String request)
{
    //Trying to handle the request

    if (request == "cheese")
    {
        //Succesful try

        System.out.println("Mouse handles cheese!");
    }
    else
    {
        //Cannot handle request

        System.out.println("Mouse in unable to handle cheese" +  request + "!");

        if (next_handler != null)
        {
            //Sending request to the next handler

            next_handler.handle_request(request);
        }
        else
        {
            //If there is no handlers left, alert about crash

            System.out.println("Chain ends without success!");
        }
    }
}
}

class Wolve extends Handler
{
public void handle_request(String request)
{
    //Trying to handle the request

    if (request == "meet")
    {
        //Succesful try

        System.out.println("Wolve handles meet!");
    }
    else
    {
        //Cannot handle request

        System.out.println("Wolve in unable to handle cheese" +  request + "!");

        if (next_handler != null)
        {
            //Sending request to the next handler

            next_handler.handle_request(request);
        }
        else
        {
            //If there is no handlers left, alert about crash

            System.out.println("Chain ends without success!");
        }
    }
}
}

class Monkey extends Handler
{
public void handle_request(String request)
{
    //Trying to handle the request

    if (request == "banana")
    {
        //Succesful try

        System.out.println("Monkey handles banana!");
    }
    else
    {
        //Cannot handle request

        System.out.println("Monkey in unable to handle" +  request + "!");

        if (next_handler != null)
        {
            //Sending request to the next handler

            next_handler.handle_request(request);
        }
        else
        {
            //If there is no handlers left, alert about crash

            System.out.println("Chain ends without success!");
        }
    }
}
}
EN

回答 2

Stack Overflow用户

发布于 2012-11-06 22:28:31

尝试java COR.Chain_Of_Responsibility并使您的main方法成为static

编辑

您必须在项目的根目录下启动java...,例如,如果您的Chain_of_responsibiliy.java在/src/COR中,则启动/src

票数 6
EN

Stack Overflow用户

发布于 2012-11-06 22:47:23

首先,确保您的目录结构与包结构相匹配。这意味着,由于您的类位于名为COR的包中,因此源文件应位于名为COR的目录中。

因此,假设您的项目在C:\MyProject中,您应该有一个名为C:\MyProject\COR的目录,其中包含源文件Chain_of_Responsibility.java

然后从目录C:\MyProject编译并运行它

代码语言:javascript
复制
C:\MyProject> javac COR\Chain_of_Responsibility.java
C:\MyProject> java COR.Chain_of_Responsibility

注意:javac命令需要源文件的路径;java命令需要完全限定的类名(而不是类文件的路径)。

如果上面的方法不起作用,那么很可能设置了CLASSPATH环境变量。您可以通过使用-cp选项显式指定类路径来覆盖它:

代码语言:javascript
复制
C:\MyProject> java -cp . COR.Chain_of_Responsibility

(而且,您的main方法必须是static,正如其他人已经指出的那样)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13253100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档