Packaging

I have a strange problem.

Any one plz help me.

I created a file

Employee.java

package com.horstmann.corejava;

import java.util.*;

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);

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 final String name;

private double salary;

private Date hireDay;

}

I compiled it, a class file was created in the directory structure /com/horstmann/corejava/Employee.class

No i have a file

//PackageTest.java

import com.horstmann.corejava.*;

public class PackageTest {

public static void main(String[] args) {

Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);

harry.raiseSalary(5);

System.out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());

}

}

When i compile this file it gives a strange error:

PackageTest.java [24:1] cannot access Employee

bad class file: C:\Documents and Settings\KA17603\Desktop\Core Java 2\Employee.java

file does not contain class Employee

Please remove or make sure it appears in the correct subdirectory of the classpath.

Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);

^

1 error

But when i replace

import com.horstmann.corejava.*; statement to

import com.horstmann.corejava.Employee;

It works fine.

Why is this so?

Can anyone help me!

Regards

Kalyan

[1972 byte] By [kalyan_anumolu] at [2007-9-30 18:12:33]
# 1
Do you have the packaged version on your classpath?
paulcw at 2007-7-6 18:44:35 > top of Java-index,Developer Tools,Java Compiler...
# 2

> When i compile this file it gives a strange error:

>

> PackageTest.java [24:1] cannot access Employee

> bad class file: C:\Documents and

> Settings\KA17603\Desktop\Core Java 2\Employee.java

> file does not contain class Employee

> Please remove or make sure it appears in the correct

> subdirectory of the classpath.

> Employee harry = new Employee("Harry Hacker", 50000,

> 1989, 10, 1);

> ^

> 1 error

>

> But when i replace

>

> import com.horstmann.corejava.*; statement to

> import com.horstmann.corejava.Employee;

>

> It works fine.

> Why is this so?

Here is what I think is happening. When compiling, the directory com/horstmann/corejava is in the Classpath. When you use "import com.horstmann.corejava.*; " the compiler doesn't know that Employee means com.horstmann.corejava.Employee. So it finds Employee.class (perhaps because it is in the current directory) and tries to use it. However, the file Employee.class does not contain plain Employee, it contains com.horstmann.corejava.Employee so you get an error. When you use "import com.horstmann.corejava.Employee;" this can not happen.

So the solution is, do not include directories in the Classpath unless you want javac to search them.

atmguy at 2007-7-6 18:44:35 > top of Java-index,Developer Tools,Java Compiler...