首页 > 代码库 > IDEA 错误:找不到或无法加载主类

IDEA 错误:找不到或无法加载主类

下的java核心编程的源码,只有java文件,没有idea或者eclipse的项目结构信息.

分别用eclipse和idea打开了一遍,方便学习调试.

项目文件夹:E:\学习资料\Java\语法\java核心技术卷1卷2源码\corejava

有些可以直接运行,有些因为源码结构问题,需要改一改结构.

比如这个

import java.util.*;

/**
 * This program tests the Employee class.
 * @version 1.11 2004-02-19
 * @author Cay Horstmann
 */
public class EmployeeTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee[] staff = new Employee[3];

      staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

      // raise everyone‘s salary by 5%
      for (Employee e : staff)
         e.raiseSalary(5);

      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
               + e.getHireDay());
   }
}

class Employee
{
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public Date getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}

没有显示声明package,用默认package,但是其他文件夹也这样,首先employee重复定义,其次我记得java的类名应该和文件名一样,除了嵌套类.

一.首先是eclipse

直接open projects from file system,不勾选nested,打开之后在有main的java文件右键, 运行.

二.idea

这个麻烦一点

首先open,选择corejava,要运行的类,比如welcome,因为welcome没有包,只能选择welcome文件夹,右键mark dir as source root,这样welcome.java打开,有个运行的绿色箭头,运行,但是提示compile path需要配置-IDEA 错误:找不到或无法加载主类

点击工具栏右侧倒数第二个,选择project structure,在project分类下的project compile out,E:\学习资料\Java\语法\java核心技术卷1卷2源码\corejava\out

同时modules-path-compile output-inherit(这个应该是默认的)

这样就可以运行了.

 

IDEA 错误:找不到或无法加载主类