Problem with first java program

M

M Wilson

Hi,
Just started learning java and can't get the following example to work. It
compiles OK but returns the following error when run:
Exception in thread "main" java.lang.NoClassDefFoundError: Motorcycle/class
I would be grateful for any help
Thanks, Matt



class Motorcycle {

String make;
String colour;
boolean engineState;

void startEngine() {
if (engineState == true)
System.out.println("The engine is already on.");
else {
engineState = true;
System.out.println("The engine is now on.");
}
}

void showAtts() {
System.out.println("This motorcycle is a " + colour + " " + make);
if (engineState == true)
System.out.println("The engine is on.");
else System.out.println("The engine is off.");
}

public static void main (String args[]) {
Motorcycle m = new Motorcycle();
m.make = "Yamaha RZ350";
m.colour = "yellow";
System.out.println("Calling showAtts...");
m.showAtts();
System.out.println("-----");
System.out.println("Starting engine...");
m.startEngine();
System.out.println("-----");
System.out.println("Calling showAtts...");
m.showAtts();
System.out.println("-----");
System.out.println("Starting engine...");
m.startEngine();
}

}
 
A

Andrew Thompson

M said:
Hi,
Just started learning java and can't get the following example to
work. It compiles OK but returns the following error when run:
Exception in thread "main" java.lang.NoClassDefFoundError:
Motorcycle/class I would be grateful for any help

It sounds like you are typing something like..
java Motorcycle.class

Try this instead..
java -cp . Motorcycle

Another piec of advice is that for the
moment you would be better off posting
to c.l.j.help
 
K

kaspar minosiants

M said:
Hi,
Just started learning java and can't get the following example to work. It
compiles OK but returns the following error when run:
Exception in thread "main" java.lang.NoClassDefFoundError: Motorcycle/class
I would be grateful for any help
Thanks, Matt



class Motorcycle {

String make;
String colour;
boolean engineState;

void startEngine() {
if (engineState == true)
System.out.println("The engine is already on.");
else {
engineState = true;
System.out.println("The engine is now on.");
}
}

void showAtts() {
System.out.println("This motorcycle is a " + colour + " " + make);
if (engineState == true)
System.out.println("The engine is on.");
else System.out.println("The engine is off.");
}

public static void main (String args[]) {
Motorcycle m = new Motorcycle();
m.make = "Yamaha RZ350";
m.colour = "yellow";
System.out.println("Calling showAtts...");
m.showAtts();
System.out.println("-----");
System.out.println("Starting engine...");
m.startEngine();
System.out.println("-----");
System.out.println("Calling showAtts...");
m.showAtts();
System.out.println("-----");
System.out.println("Starting engine...");
m.startEngine();
}

}
I works fine
you should run it java Motorcycle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top