我把代码分到了两个程序里EmployeeText.java和Employee.java
EmployeeText.javapackage text;
public class EmployeeText {
public static void main(String[] args) 
{
Employee[] staff=new Employee[3];
staff[0]=new Employee("harry",2500,2010,7,20);
staff[1]=new Employee("lives",3500,2008,3,12);
staff[2]=new Employee("cikens",7000,2007,4,11);

for(Employee e:staff)
e.raisesalary(5);

for(Employee e:staff)
System.out.println(e.getname()+"  "+e.getsalary()+"  "+e.gethireday());
}}
==================================
Employee.javapackage text;
import java.util.*;
class Employee 
{
//构造函数
public Employee(String aname,double asalary,int year,int month,int day)
{
name=aname;
salary=asalary;
GregorianCalendar Calendar=new GregorianCalendar(year,month-1,day);
hireday=Calendar.getTime();
}
//提供公共方法访问值
public String getname(){return name;}
public double getsalary(){return salary;}
public Date gethireday(){return hireday;}

public void raisesalary(double percent){salary+=salary*percent/100;}


private String name;
private double salary;
private Date hireday; static{System.out.println("hello");System.exit(0);}
}
在CMD里运行CD D:
cd D:\eclipse\item\employeetext\src\text
javac Employee.java
java Employee
显示Exception in thread "main" java.lang.NoClassDefFoundError:
换成EmployeeText一样 求助在eclipse中运行只显示hello,只有把static{System.out.println("hello");System.exit(0);}去掉后才能正常显示,求助为什么,书上说第一次加载类时会自动运行初始块,可这为什么只运行初始块

解决方案 »

  1.   

    System.exit(0)是停掉JVM
    jvm是什么?所有java程序运行的根基。根基都没了,你说呢
      

  2.   

    关键是这句: System.exit(0);下手也太狠了,斩草除根,你还怎么运行下去啊
      

  3.   

    在text的parent目录, 运行 java text.EmployeeText
      

  4.   

    EmployeeText和Employee  两个类都要编译
    运行EmployeeText这个类就不报错了
    main线程走到Employee[] staff=new Employee[3];的时候 会初始化Employee类就会执行静态代码块,而你的代码块System.exit(0);就将JVM给停止了,所以程序就不会往下走了
      

  5.   

    不明白为什么在static块中加一句 System.exit(0);