Reputation: 896
My question is the following: I have a java code, written in one .java file - Chain_of_Responsibility, the code is in the end of my question. I compiled it on Linux with
javac Chain_of_Responsibility.java
And get all my .class files in same directory. Then I try to run my program with
java Chain_of_Responsibility
And get " exception in thread "main" java.lang.NoClassDefFoundError: Chain_of_Responsibility". I've try to make my main function static, write all my classes in different .java files, but without success. So I have no idea what to do. Can you help me?
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!");
}
}
}
}
Upvotes: 1
Views: 1748
Reputation: 2174
Try java COR.Chain_Of_Responsibility
and make your main
method static
EDIT
you have to launch java...
at the root of your project, e.g. /src
if your Chain_of_responsibiliy.java is in /src/COR
Upvotes: 6
Reputation: 206796
First of all, make sure that your directory structure matches your package structure. This means that, since your class is in a package named COR
, the source file should be in a directory named COR
.
So, assuming that your project is in C:\MyProject
, you should have a directory named C:\MyProject\COR
that contains the source file Chain_of_Responsibility.java
.
Then compile and run it from the directory C:\MyProject
:
C:\MyProject> javac COR\Chain_of_Responsibility.java
C:\MyProject> java COR.Chain_of_Responsibility
Note: The javac
command expects the path to the source file; the java
command expects a fully-qualified class name (not the path to the class file).
If the above doesn't work, then you most likely have the CLASSPATH
environment variable set. You can override that by explicitly specifying the classpath with the -cp
option:
C:\MyProject> java -cp . COR.Chain_of_Responsibility
(Also, your main
method must be static
, as others have already noted).
Upvotes: 0