Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question on package naming vs. directories  RSS feed

 
Fritz Urling
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I admit that I am weak when it comes to packages. But I always thought that the package name in the .java file had to match your directory structure.

We have some files with the following:



but the directory structure is



I'm using eclipse to do my builds, and it complains that the package declaration doesn't match the expected package name.

However, ANT builds things just fine. When we open up the package, we see in there both a lpname directory with the .class files, and the longpackagename directory with the .java files.

Does Eclipse enforce something that ANT doesn't? do we have ANT configured wrong? Or do I just have a basic flaw in my understanding of how the world works???
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i know packages and directories must match in order to compile and i think this does not depends of the ide but is the way javac works.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The "rule" about packages and paths isn't really a rule; the Java Language Specification suggests that some implementations may require this, but it's not set in stone. In general, it helps, and the default behavior of many tools is to assume that's how things are laid out. But with the proper command-line flags, many Java compilers can find source and class files that aren't stored this way. Ant is just passing the right switches to "javac" so that it can find the files. Eclipse knows where things are, but it's telling you you're doing things in a nonstandard way.

You'll actually have much more of a problem at runtime, though, if your classes are stored in improperly named directories -- most if not all JVMs want things to be stored the recommended way.

In general, it's easiest to just do things the standard way, but it's nice to know that in some situations you can bend javac to your will if needed.
 
Fritz Urling
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. so in the end it's a configuration issue. apparently we have ANT set up to allow it, but not my Eclipse.

We ultimatly changed the package statement to match our directories, to preserve our sanity. it was just odd seeing it work in some places but not others. we were worried it might have been a "tip of the iceberg", but now i think we can all sleep safely.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!