2012-11-06 62 views
1

我的问题是以下内容: 我有一个java代码,写在一个.java文件 - Chain_of_Responsibility,代码是在我的问题结束。 我java:线程“main”中的异常java.lang.NoClassDefFoundError:Chain_of_Responsibility

javac Chain_of_Responsibility.java 

编译它在Linux和获得相同的目录中的所有我的.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!"); 
     } 
    } 
} 
} 
+0

你会在不久的将来接受答案吗? – Jerome

回答

6

尝试java COR.Chain_Of_Responsibility,让你main方法static

编辑

,你必须在你的项目,例如根推出java.../src如果您的Chain_of_responsibiliy.java位于/src/COR

+4

和请,请不要用下划线写你的类名... – dounyy

+0

@dounyy从这种习俗哪种语言? PHP? –

+2

...并使用**小写**包名... –

0

首先,确保您的目录结构与您的包结构匹配。这意味着,由于您的课程名为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,正如其他人已经指出的那样)。

+0

非常感谢!有用! –

相关问题