我问一个简单的问题;
如下两个类
PackageTest.javaimport com.horstmann.corejava.*;
// the Employee class is defined in that packageimport static java.lang.System.*;/**
* This program demonstrates the use of packages.
* @author cay
* @version 1.11 2004-02-19
* @author Cay Horstmann
*/
public class PackageTest
{
public static void main(String[] args)
{
// because of the import statement, we don't have to use com.horstmann.corejava.Employee here
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1); harry.raiseSalary(5); // because of the static import statement, we don't have to use System.out here
out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
}
}Employee.java
///////////
package com.horstmann.corejava;// the classes in this file are part of this packageimport java.util.*;// import statements come after the package statement/**
* @version 1.10 1999-12-18
* @author Cay Horstmann
*/
public class Employee
{
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;
} private String name;
private double salary;
private Date hireDay;
}也就是 第一个类调用了Employee这个类,这个类定义在
package com.horstmann.corejava在window环境中,我用命令行编译
javac PackageTest.java报一下错误,是什么原因PackageTest.java:1: package com.horstmann.corejava does not exist
import com.horstmann.corejava.*;
^
PackageTest.java:17: cannot find symbol
symbol : class Employee
location: class PackageTest
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
^
PackageTest.java:17: cannot find symbol
symbol : class Employee
location: class PackageTest
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
^
3 errors
如下两个类
PackageTest.javaimport com.horstmann.corejava.*;
// the Employee class is defined in that packageimport static java.lang.System.*;/**
* This program demonstrates the use of packages.
* @author cay
* @version 1.11 2004-02-19
* @author Cay Horstmann
*/
public class PackageTest
{
public static void main(String[] args)
{
// because of the import statement, we don't have to use com.horstmann.corejava.Employee here
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1); harry.raiseSalary(5); // because of the static import statement, we don't have to use System.out here
out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
}
}Employee.java
///////////
package com.horstmann.corejava;// the classes in this file are part of this packageimport java.util.*;// import statements come after the package statement/**
* @version 1.10 1999-12-18
* @author Cay Horstmann
*/
public class Employee
{
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;
} private String name;
private double salary;
private Date hireDay;
}也就是 第一个类调用了Employee这个类,这个类定义在
package com.horstmann.corejava在window环境中,我用命令行编译
javac PackageTest.java报一下错误,是什么原因PackageTest.java:1: package com.horstmann.corejava does not exist
import com.horstmann.corejava.*;
^
PackageTest.java:17: cannot find symbol
symbol : class Employee
location: class PackageTest
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
^
PackageTest.java:17: cannot find symbol
symbol : class Employee
location: class PackageTest
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
^
3 errors
javac -d 路径名 java源程序文件名
我使用正常啊
E:\Editplus>javac PackageTest.javaE:\Editplus>java PackageTest
name=Harry Hacker,salary=52500.0
直接package com.horstmann.corejava;不就可以啦。不在同一个包里,这个程序是正确的啊
目录机构
c:\Package\PackageTest.java
c:\Package\com\horstmann\corejava\Employee.java
javac PackageTest.java就可以了,我认为PackageTest.java会自己去找需要的类,但是不行
我开始以为是程序的问题,但是同样的程序,在eclipse是可以的。
eclipse是可以自动编译*.java程序的。