A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

运行时老是出现这种情况,怎么办?是设置的问题嘛

QQ截图20140209230234.png (146.79 KB, 下载次数: 13)

QQ截图20140209230234.png

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

8 个回复

倒序浏览
当你编译源文件 以后都会生成一些类
执行时要用java(main所在的类)
如果找不到相应的类,执行就会出现NoClassDefFoundError的异常
回复 使用道具 举报
1.首先javac编译你的.Java文件,
2.然后再Java编译.class文件,这时候就不用写后缀名了。
回复 使用道具 举报
  1. import java.util.*;
  2. //由于Student对象不具备比较性,无法存入TreeSet集合中,所以要实现Comparable,实现compareTo方法
  3. public class TreeSetTest//无法编译,不知怎么回事儿?
  4. {
  5.         public static void main(String args[]){

  6.                 TreeSet<Student> ts=new TreeSet<Student>();

  7.                 ts.add(new Student("小白",21));
  8.                 ts.add(new Student("小黄",20));
  9.                 ts.add(new Student("小黑",21));
  10.                 ts.add(new Student("小红",19));
  11.                 ts.add(new Student("小白",21));

  12.                 /*Exception in thread "main" java.lang.ClassCastException:
  13.                                 Student cannot be cast to java.lang.Comparable        由于Student对象没有比较性
  14.                 */
  15.                 Iterator it=ts.iterator();        
  16.                 while(it.hasNext()){        //打印
  17.                                 Student s=(Student)it.next();
  18.                                 sop(s.getName()+".."+s.getAge());
  19.                 }
  20.                        
  21.         }
  22.         public static void sop(Object obj){
  23.                         System.out.println(obj);
  24.         }

  25. }


  26. class Student implements Comparable        //该接口强制让Student具有比较性
  27. {
  28.                 private String name;
  29.                 private int age;

  30.                 Student(String name,int age){
  31.                                 this.name=name;
  32.                                 this.age=age;
  33.                 }

  34.                 //实现compareTo方法
  35.                 public int compareTo(Object obj){
  36.                                 if(!(obj instanceof Student))
  37.                                                 throw new RuntimeException("不是学生对象");
  38.                                 Student t=(Student)obj;
  39.                                 System.out.println(this.name+".."+t.name);
  40.                                 if(this.age>t.age)                //当调用对象年龄年龄大于比较对象时
  41.                                                 return 1;
  42.                                 if(this.age==t.age){        //当调用对象年龄等于比较对象时,再比较姓名是否相等
  43.                                 //        System.out.println(this.name+".."+t.name);
  44.                                                 if(this.name.equals(t.name))
  45.                                                                 return 0;
  46.                                 }
  47.                                 return -1;        //年龄小于比较对象返回-1
  48.                 }

  49.                 String getName(){
  50.                                 return name;
  51.                 }
  52.                 int getAge(){
  53.                                 return age;
  54.                 }
  55. }
  56. import java.util.*;
  57. //由于Student对象不具备比较性,无法存入TreeSet集合中,所以要实现Comparable,实现compareTo方法
  58. public class TreeSetTest//无法编译,不知怎么回事儿?
  59. {
  60.         public static void main(String args[]){

  61.                 TreeSet<Student> ts=new TreeSet<Student>();

  62.                 ts.add(new Student("小白",21));
  63.                 ts.add(new Student("小黄",20));
  64.                 ts.add(new Student("小黑",21));
  65.                 ts.add(new Student("小红",19));
  66.                 ts.add(new Student("小白",21));

  67.                 /*Exception in thread "main" java.lang.ClassCastException:
  68.                                 Student cannot be cast to java.lang.Comparable        由于Student对象没有比较性
  69.                 */
  70.                 Iterator it=ts.iterator();        
  71.                 while(it.hasNext()){        //打印
  72.                                 Student s=(Student)it.next();
  73.                                 sop(s.getName()+".."+s.getAge());
  74.                 }
  75.                        
  76.         }
  77.         public static void sop(Object obj){
  78.                         System.out.println(obj);
  79.         }

  80. }


  81. class Student implements Comparable        //该接口强制让Student具有比较性
  82. {
  83.                 private String name;
  84.                 private int age;

  85.                 Student(String name,int age){
  86.                                 this.name=name;
  87.                                 this.age=age;
  88.                 }

  89.                 //实现compareTo方法
  90.                 public int compareTo(Object obj){
  91.                                 if(!(obj instanceof Student))
  92.                                                 throw new RuntimeException("不是学生对象");
  93.                                 Student t=(Student)obj;
  94.                                 System.out.println(this.name+".."+t.name);
  95.                                 if(this.age>t.age)                //当调用对象年龄年龄大于比较对象时
  96.                                                 return 1;
  97.                                 if(this.age==t.age){        //当调用对象年龄等于比较对象时,再比较姓名是否相等
  98.                                 //        System.out.println(this.name+".."+t.name);
  99.                                                 if(this.name.equals(t.name))
  100.                                                                 return 0;
  101.                                 }
  102.                                 return -1;        //年龄小于比较对象返回-1
  103.                 }

  104.                 String getName(){
  105.                                 return name;
  106.                 }
  107.                 int getAge(){
  108.                                 return age;
  109.                 }
  110. }

复制代码
这是我的代码,上面是运行截图
回复 使用道具 举报
复制了两遍
回复 使用道具 举报
应该是你没有编译,或者编译后的class文件不在本文件夹
回复 使用道具 举报
看提示大哥,类没有找到的错误,这个问题太简单了
以下来自百度:
“没有找到这样的类”错误
表面上看是类的名称错误,其实更有可能是classpath环境变量没有配置好。

建议复查类的名称包括大小写,如果有包的情况下,注意类文件放的位置。
环境变量classpath配置中有没有小数点“.”,有没有java默认类库的位置,有没有你自己的类文件顶层目录等。

评分

参与人数 1黑马币 +2 收起 理由
zzkang0206 + 2 山寨

查看全部评分

回复 使用道具 举报
“没有找到这样的类”错误
表面上看是类的名称错误,其实更有可能是classpath环境变量没有配置好。

建议复查类的名称包括大小写,如果有包的情况下,注意类文件放的位置。
环境变量classpath配置中有没有小数点“.”,有没有java默认类库的位置,有没有你自己的类文件顶层目录等。

评分

参与人数 1黑马币 +2 收起 理由
zzkang0206 + 2 山寨

查看全部评分

回复 使用道具 举报
谢谢各位
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马