views:

400

answers:

4

We have a package that ends with exception e.g.

package a.b.c.exception;

Our code base had no issues up till eclipse 3.3, however when we shifted to eclipse 3.4, it started giving errors related to this package:

"The package a.b.c.exception collides with a type"

When I refactor the package name to a.b.c.exceptions, there are no issues. Is this due to a bug in eclipse 3.4 or is there some setting to rectify this behavior?

+1  A: 

In Java you can not have a class name that is the same as a package name.

That means the JDT package must have enforced that rule only in 3.4

See bug 63668 for instance.


As Nate comments:

A class named Exception won't prevent you from creating package exception.
Case matters.

Also remember the full name of a class includes the package it's in.
So a.b.SomeClass (class name) is different from x.y.SomeClass (package name).
There would be no name collision here.

The class name and the package name have to match in both case and package to cause this error.

See his more accurate answer.

VonC
The classes in this package have different names.Package name is a.b.c.exceptionClasses in this package: ABCException, DEFException, GHIExceptionOr are you saying because Java has a class/type Exception, therefore I cannot create a package named 'exception'?
Monis Iqbal
@Monis: the latter: class `Exception` in Java should prevent you to create a package `exception`.
VonC
I would disagree b/c I have another package x.y.exception which does not give this collision with type issue.
Monis Iqbal
A class named `Exception` won't prevent you from creating package `exception`. Case matters. Also remember the full name of a class includes the package it's in. So a.b.SomeClass (class name) is different from x.y.SomeClass (package name). There would be no name collision here. The class name and the package name have to match in both case and package to cause this error.
Nate
right, agreed. Any pointers to what the issue is then? To me it looks related to 3.4.
Monis Iqbal
+2  A: 

It's because you have a class named exception (with a lower case "e") in the a.b.c package and a package named a.b.c.exception.

It causes a name collision because if you have the code a.b.c.exception.doSomething(); - does that mean you want to call the static doSomething() method in the a.b.c.exception class? Or does it mean there's a class called a.b.c.exception.doSomething that you're trying to invoke the constructor of?

Stick with the Java naming conventions - packages all lowercase, classes starting with an uppercase and camel-case after - and you'll never see this problem.

==========EDIT==========

This is the only legitimate reason this error should be showing up...

It doesn't have to be in your project directly, it could be in another project or library that your project depends on. This should show you any occurrences of the class anywhere on the build path or your project : Hit the Flashlight looking button in the Eclipse toolbar -> Choose 'Java Search' -> enter a.b.c.exception in search field -> select 'Case sensitive' -> select 'Type' in 'Search For' -> make sure all options are selected for 'Search In'.

Are you using any tools that generate classes? Could they be putting them into the build directory of your project? When you see the error, if you go to the project's build directory, and go down into the a/b/c/ directory do you see a .class file for 'exception'?

Of course Eclipse in general could have a bug (though I'd expect there would be a bug report in Eclipse 3.4 and you'd be able to find more complaints if it was...), your Eclipse install could be broken in some way (Can anyone else open your project in Eclipse 3.4? Could you do a clean Eclipse 3.4 install in another directory? Does the error appear there?), or your project could be messed up in some way (Create a new project with no dependencies other than the JDK, create the a.b.c.exception package in your new project, create a class in your project to import a.b.c.exception.*; and see if the error occurs.).

Nate
nops, no class named exception or Exception in the package or anywhere else. We are quite strictly sticking to the Java conventions :)
Monis Iqbal
After seeing your comment, (and this answer), I agree. Much more accurate. +1
VonC
Nate/VonC do you still think that this theory explains the problem I'm stating?
Monis Iqbal
Edited answer body.
Nate
Nate, thanks for explaining the possibilities. I couldn't find the type that this package is colliding with. However, when I created package with the same name in another workspace, it worked without issues.Also, in this case too when I start clean-building my workspace the error vanishes and in the end when building completes, the error crops up again. Do you think there is any way I can find the location of the type that's colliding with my package?
Monis Iqbal
A: 

I changed one of the compilation option in eclipse and the problem disappeared. Under workspace properties: Java Compiler -> Errors/Warnings -> Change 'Unused import' from 'Warning' to 'Ignore'.

Monis Iqbal
although this solved the issue I was facing but i'm not able to determine why only this particular package gave the error in the first place. There were many other packages which do not have immediate classes.
Monis Iqbal
+1  A: 

I encountered a similar problem in a huge code base that I inherited. It turns out that the clash was caused by an partially qualified class name in a JavaDoc link.

To paraphrase, Eclipse was telling me that I had a package/type clash for a.b.c.d. when compiling a.b.c.d.London. Doing a java search on the code for a.b.c.d revealed that Eclipse thought that a JavaDoc comment in a.b.c.Paris was a match. The JavaDoc comment contained {@ link d.NewYork}. When I changed the it to read {@link a.b.c.d.NewYork} the compilation error was resolved.

It should also be noted that NewYork was not imported into the Paris class as it only appeared in the JavaDoc comment. This also made it un-resolved in its abbreviated form and clicking on the link in the comment did not work. Making it an absolute reference also makes the JavaDoc link work.