java: 线程 "main"java.lang.NoClassDefFoundError: Chain_of_Responsibility 中的异常

标签 java linux exception

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

编译了它
javac Chain_of_Responsibility.java

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

java Chain_of_Responsibility

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

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!");
        }
    }
}
}

最佳答案

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

编辑

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

关于java: 线程 "main"java.lang.NoClassDefFoundError: Chain_of_Responsibility 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13253100/

相关文章:

Java 在 Mac OS X 中最大化或全屏窗口后停止捕获鼠标移动事件

c# - 具有大或*非常*大文件的 Resharper

linux - Gitlab避免用户通过SSH登录

visual-studio - Visual Studio 2019(本身): Exception Has Been Thrown By The Target of an Invocation

c++ - 从 Q_INVOKABLE 方法中抛出异常?

java - 如果不是int,该如何捕捉用户输入?

java - 使用流将 Map<String,Set<String>> 展平为 List<String>(CSV 转换)

java泛型,未经检查的警告

linux - 用于在前台但在不同终端选项卡上运行进程的脚本

linux - gdb 地址太短