【问题标题】:java: exception in thread "main" java.lang.NoClassDefFoundError: Chain_of_Responsibilityjava:线程“main”中的异常 java.lang.NoClassDefFoundError:Chain_of_Responsibility
【发布时间】:2012-11-06 22:48:13
【问题描述】:

我的问题如下: 我有一个 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 linux exception


【解决方案1】:

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

编辑

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

【讨论】:

  • @dounyy 这个习惯来自哪种语言? PHP?
  • @JanDvorak 我不知道,但是这个 java 扬声器的口音很糟糕
  • 我将 main 方法设为静态,运行 java COR.Chain_of_Responsibility,然后得到“错误:无法找到或加载主类 COR.Chain_of_Responsibility”。
  • 我的 Java 口音怎么样,我是自学的,我在 C++ 程序中保留的传统仍然很强大。
【解决方案2】:

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

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

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

C:\MyProject> javac COR\Chain_of_Responsibility.java
C:\MyProject> java COR.Chain_of_Responsibility

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

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

C:\MyProject> java -cp . COR.Chain_of_Responsibility

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

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多