-1

I’m having a problem with Eclipse (2023-03 and earlier versions) and its compiler. I have a project here that's configured correctly (in terms of build tools, source paths, etc.), but Eclipse has a problem with one of the files, and yes, the directory the source code of this specific file is in does indeed not match the declared package inside the file.

However, in this specific instance it is necessary to do that, and Java itself doesn't tell you anywhere that the source code for class a.b.c.D has to be in directory a/b/c.

How do I disable the compiler error?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bombe
  • 81,643
  • 20
  • 123
  • 127
  • 3
    As far as I know, you can't. Also, I have never seen a case where _"it is necessary to do that"_, so I'm interested in the underlying reasons for this. – Mark Rotteveel Aug 29 '23 at 10:31
  • Related, possibly duplicate: [The declared package does not match the expected package ""](https://stackoverflow.com/questions/6997524/the-declared-package-does-not-match-the-expected-package) – Mark Rotteveel Aug 29 '23 at 10:32
  • You might be right that "Java itself doesn't tell you anywhere that the source code for class `a.b.c.D` has to be in directory `a/b/c`", meaning that it is not clearly said in the Java language specification (although I still have to check that …). But I am relatively sure that the manual page for *javac* says that. – tquadrat Aug 29 '23 at 10:52
  • 2
    This is not quite true. The Java language specification actually tells: [_"Modules and packages that are stored in a file system may have certain constraints on the organization of their compilation units to allow a simple implementation to find module, class, and interface declarations easily."_](https://docs.oracle.com/javase/specs/jls/se17/html/jls-7.html#jls-7.3). What your are doing can be treated as invalid Java, so better avoid it. – howlger Aug 29 '23 at 11:55
  • There is no way to do this, Bombe. For good reason: There is no reason to ever need to. Whatever makes you say 'I have a project here that's configured correctly' - no it isn't. To get more meaningful answers than 'you can't, and you're doing it wrong' - perhaps expand a bit on what specific parts of the setup that are causing issues require you to put things in the wrong place. As @howlger shows, the part of the spec you think says what you are doing is fine, is misleading. Hence why just "I follow spec, eclipse does not work, please help" as a question isn't going to work here. – rzwitserloot Aug 29 '23 at 12:11
  • No, the spec says “_may_” – which means that it’s kind of up to the host system to define how it locates compilation units. And javac will happily compile source code where the package declaration does not match the filesystem path. But, oh well, it’s not like I really _needed_ another reason to not use Eclipse, but I guess, thanks to Eclipse for providing another one? :/ – Bombe Aug 29 '23 at 19:45
  • Host system can be any tool that works on the source code. I'm probably not the only who wasted time where the name of a non-public class didn't match the filename, which is completely legal according to the Java language specification. Therefore I'm also not happy that javac happily compiles cases where the package name does not match the folder structure without any warning. You still haven't answered why you cannot correct your project structure . Whatever, you can use the Eclipse resource link feature to map the folders to the right place without changing the structure on the file system. – howlger Aug 30 '23 at 08:30

0 Answers0