interface HasBatteries{}
interface Waterproof{}
interface Shoots{}class Toy{
Toy(){
}
Toy(int i){
}
}
class FancyToy extends Toy implements HasBatteries,Waterproof,Shoots {
public FancyToy(){
super(1);
}
}
public class ToyTest { public ToyTest() {}
static void printInfo(Class cc){
System.out.println("class name:"+cc.getName()+"is interface?["+cc.isInterface()+"]");
System.out.println("Simple name:"+cc.getSimpleName());
System.out.println("Canonical name:"+cc.getCanonicalName());
}
public static void main(String[] args){
Class c=null;
try{
c=Class.forName("typeinfo.toys.fancyToy");
}catch(ClassNotFoundException e){
System.out.println(e.toString());
System.exit(1);
}
printInfo(c);
for(Class face:c.getIntegerfaces())
printInfo(face);
Class up=c.getSuperclass();
Object obj=null;
try{
obj=up.newInstance();
}catch(InstantiationException e){
System.out.println("Cannot instantiation");
System.exit(1);
}catch(IllegalAccessException e){
System.out.println("Cannot access");
System.exit(1);
}
printInfo(obj.getClass());
}
}
以上是Thinking in java(4th)中的一段代码,但是在我的机子上不能运行,会报如下错误:
java.lang.NoClassDefFoundError: ToyTest
Exception in thread "main"
经过测试,是for(Class face:c.getIntegerfaces())导致的错误,我的JVM是jdk1.5.0,是版本的问题吗?
interface Waterproof{}
interface Shoots{}class Toy{
Toy(){
}
Toy(int i){
}
}
class FancyToy extends Toy implements HasBatteries,Waterproof,Shoots {
public FancyToy(){
super(1);
}
}
public class ToyTest { public ToyTest() {}
static void printInfo(Class cc){
System.out.println("class name:"+cc.getName()+"is interface?["+cc.isInterface()+"]");
System.out.println("Simple name:"+cc.getSimpleName());
System.out.println("Canonical name:"+cc.getCanonicalName());
}
public static void main(String[] args){
Class c=null;
try{
c=Class.forName("typeinfo.toys.fancyToy");
}catch(ClassNotFoundException e){
System.out.println(e.toString());
System.exit(1);
}
printInfo(c);
for(Class face:c.getIntegerfaces())
printInfo(face);
Class up=c.getSuperclass();
Object obj=null;
try{
obj=up.newInstance();
}catch(InstantiationException e){
System.out.println("Cannot instantiation");
System.exit(1);
}catch(IllegalAccessException e){
System.out.println("Cannot access");
System.exit(1);
}
printInfo(obj.getClass());
}
}
以上是Thinking in java(4th)中的一段代码,但是在我的机子上不能运行,会报如下错误:
java.lang.NoClassDefFoundError: ToyTest
Exception in thread "main"
经过测试,是for(Class face:c.getIntegerfaces())导致的错误,我的JVM是jdk1.5.0,是版本的问题吗?
for(Class face:c.getIntegerfaces())
改为:
for(Class face:c.getInterfaces())
c=Class.forName("typeinfo.toys.fancyToy");
改为:
c=Class.forName("typeinfo.toys.FancyToy");
还有你需要代码最前面加个package typeinfo.toys;