1

私の質問は次のとおりです。1つの.javaファイルに書かれたJavaコードがあります-Chain_of_Responsibility、コードは私の質問の最後にあります。Linuxでコンパイルしました

javac Chain_of_Responsibility.java

そして、すべての .class ファイルを同じディレクトリに取得します。次に、プログラムを実行しようとします

java Chain_of_Responsibility

そして、「スレッド「メイン」 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!");
        }
    }
}
}
4

2 に答える 2

6

java COR.Chain_Of_Responsibilityあなたのmain方法を試してみてくださいstatic

編集

java...プロジェクトのルートで起動する必要があります。たとえば/src、Chain_of_responsibiliy.java が/src/COR

于 2012-11-06T14:28:31.433 に答える
0

まず、ディレクトリ構造がパッケージ構造と一致していることを確認してください。これは、クラスが という名前のパッケージに含まれているCORため、ソース ファイルは という名前のディレクトリにある必要があることを意味しますCOR

したがって、プロジェクトが にあると仮定すると、ソース ファイルを含むC:\MyProjectという名前のディレクトリが必要です。C:\MyProject\CORChain_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

于 2012-11-06T14:47:22.650 に答える