Bug 328398 - Eclipse won't compile classes with wrong package name
Summary: Eclipse won't compile classes with wrong package name
Status: VERIFIED DUPLICATE of bug 16209
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: 3.7 M3   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-21 15:30 EDT by Maarten Coene CLA
Modified: 2010-10-26 13:06 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Maarten Coene CLA 2010-10-21 15:30:29 EDT
Build Identifier: I20100805-1700

Hi,

when I try to create a class with a wrong package name (the package name doesn't match the directory containing the java file), Eclipse will underline the package name telling that the declared package doesn't match the expected package and the class won't get compiled.

However, I can compile this java file using the standard javac compiler that ships with JDK 1.6.

I know it's not a good practise to write such code, but I think Eclipse shouldn't throw an error on it, or this behaviour should be configurable using the "Java Compiler > Error/warnings"-settings.

Maarten

Reproducible: Always

Steps to Reproduce:
1. create a new Java Project
2. create a new package 'bar'
3. create a new class inside this package named 'Foo'.
4. type this as content of this new class:
=== start Foo.java ===
package bar.boo;

public class Foo {
}
=== end Foo.java ===

=> Eclipse will underline the package declaration and won't compile this class.
Comment 1 Ayushman Jain CLA 2010-10-22 06:12:43 EDT
Although javac does allow the user to compile with an incorrect package declaration, eclipse can't do that because it has to do in-time compilation of the code and also resolve the dependencies that other files may have with the file being currently compiled. An incorrect package declaration would mean that suddenly code resolution for all the other places that were using the fully qualified name for a type, etc will fail.

Also, i dont understand your motivation behind declaring a different package than the one in which the class is being created. Why not just change the package name itself? You will anyway have to change the package at some point later.

I intend to close this as WONTFIX.
Comment 2 Maarten Coene CLA 2010-10-22 06:46:11 EDT
I agree with you that you should not declare yourself a package that is inconsistent with the location on disk of the java file.

However, we are using a lot of external libraries and we don't own the source code of these libraries. I wanted to patch one of these libraries to fix a bug, so I opened the sources in my favorite IDE: Eclipse. Unfortunately, I was not able to patch the library quickly because of this problem: the library sources were having inconsistent package declarations. Moving all source files to their correct directory was just too much work so I used IntelliJ IDEA to patch the library (IDEA also has some issues with this, but at least it could compile the code). It just would have been nice if I could have done this with Eclipse as well.
Comment 3 Ayushman Jain CLA 2010-10-25 02:20:41 EDT
Srikanth, what is your take on this?
Comment 4 Srikanth Sankaran CLA 2010-10-25 03:23:06 EDT
(In reply to comment #3)
> Srikanth, what is your take on this?

See also bug 119440, bug 16209, bug 48407, bug 151316, bug 85394 et al.
As the lengthy discussion in bug 16209 shows this issue has been debated
in great detail the conclusion being it would break other parts of the IDE
and it would not be cost effective to rework things to a full solution.

*** This bug has been marked as a duplicate of bug 16209 ***
Comment 5 Jay Arthanareeswaran CLA 2010-10-26 13:06:21 EDT
Verified for 3.7M3