Exception in thread "main"

G

gianluigi beuzard

Hi, I'm trying to start a class under eclipse that return en exception in
thread "main". I executed many codes without any broken message. How to fix
that ? Thanks in advance.

# The class :
//Polymorphisme 02

class Shape {

void draw() {}
void erase() {}

}

class Circle extends Shape {

void draw() {
System.out.println("Circle.draw()");
}
void erase() {
System.out.println("Circle.erase()");
}

}

class Square extends Shape {

void draw() {
System.out.println("Square.draw()");
}
void erase() {
System.out.println("Square.erase()");
}

}

class Triangle extends Shape {

void draw() {
System.out.println("Triangle.draw()");
}
void erase() {
System.out.println("Triangle.erase()");
}

}

public class Shapes {

public static Shape randShape() {

switch((int) (Math.random()*3)) {

default:
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();

}

}

public static void main(String[] args) {

Shape[] s=new Shape[9];
//Remplissage du tableau avec des formes [shapes]:
for (int i= 0; i<s.length;i++)
s=randShape();
//Appel polymorphe des méthodes
for(int i=0;i<s.length;i++)
s.draw();

}
}

# In the console :
java.lang.NoClassDefFoundError: shape (wrong name: Shape)

at java.lang.ClassLoader.defineClass0(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.access$100(Unknown Source)

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Exception in thread "main"
 
D

Duane Evenson

Hi, I'm trying to start a class under eclipse that return en exception in
thread "main". I executed many codes without any broken message. How to fix
that ? Thanks in advance.

# The class :
//Polymorphisme 02

class Shape {

void draw() {}
void erase() {}

}

class Circle extends Shape {

void draw() {
System.out.println("Circle.draw()");
}
void erase() {
System.out.println("Circle.erase()");
}

}

class Square extends Shape {

void draw() {
System.out.println("Square.draw()");
}
void erase() {
System.out.println("Square.erase()");
}

}

class Triangle extends Shape {

void draw() {
System.out.println("Triangle.draw()");
}
void erase() {
System.out.println("Triangle.erase()");
}

}

public class Shapes {

public static Shape randShape() {

switch((int) (Math.random()*3)) {

default:
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();

}

}

public static void main(String[] args) {

Shape[] s=new Shape[9];
//Remplissage du tableau avec des formes [shapes]:
for (int i= 0; i<s.length;i++)
s=randShape();
//Appel polymorphe des méthodes
for(int i=0;i<s.length;i++)
s.draw();

}
}

# In the console :
java.lang.NoClassDefFoundError: shape (wrong name: Shape)

at java.lang.ClassLoader.defineClass0(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.access$100(Unknown Source)

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Exception in thread "main"


The trouble is with your setup of java.
Try this as a test:
Go to the directory where Shapes.class and Shape.class exist.
Run "java -cp . Shapes".
The program should now run.

The fix involves your $CLASS_PATH variable
You need to either delete your $CLASS_PATH environmental variable or, if
it points to important libraries, add your current working directories to
the path.

Good luck,
Duane
 
C

Chris Uppal

gianluigi said:
public class Shapes { [...]
public static void main(String[] args) { [...]
# In the console :
java.lang.NoClassDefFoundError: shape (wrong name: Shape)

Perhaps you have (by mistake) told Eclipse to start in class Shape, rather than
in class Shapes.

-- chris
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top