1

EDIT: Let me just take a sentence out of my original post and place it in bold at the top so that this doesn't get marked duplicate:

I understand the origin of "com" (Sun set it as a convention to use your TLD in reverse, etc, so you have a unique package name) and what I'm wondering at this point is whether ordinary solo devs with no TLD to speak of are still conventionally expected to create an encapsulating top level com folder just to meet that convention.

For example, I currently have my java files contained in a directory called JavaProject. I guess that makes the current package name JavaProject also. Do I need to place the JavaProject directory inside a parent com directory for no reason other than to look "normal"? It doesn't seem to serve any practical purpose that I can tell. Am I wrong?

Note, I am not using an IDE like Eclipse right now, I am just working with a text editor and the command line and creating my files and packages by hand.

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • 1
    Note: (conventional) directory structure follows package name, not the other way around. Package names comes from `package` statements in Java source files, without any regard whatever to source file location. – John Bollinger Jan 19 '19 at 17:35
  • @JohnBollinger thank you for bringing that up, I was actually wondering about that. But doesn't it amount to the same thing, though? You're still expected to have a directory structure that matches the package name, no? – temporary_user_name Jan 19 '19 at 17:41
  • 2
    @temporary_user_name, it amounts to the same thing only if you lay out your files in the conventional way with respect to the `package` statements inside. That makes a lot of things easier, but it is not *required*. Note that this is in response to "I guess that makes the current package name JavaProject also", because ***no***, the directory in which your sources reside is not what decides to which package the classes defined inside belong. (And good thing, too, because "JavaProject" is an absolutely horrid package name -- too generic, and uses unconventional capitalization.) – John Bollinger Jan 19 '19 at 18:02
  • Yes, of course, understood, it's just for my own keeping track of things on my desktop. Thanks. – temporary_user_name Jan 19 '19 at 18:17
  • To quote one of the comments on the duplicate (and it is a duplicate IMHO): _"I've seen people use their GitHub username as their domain, so, for example: com.guthub.pwagland.xxx [sic] the primary purpose is to get a unique name, so that you don't have to change it, and it will never conflict with the name that someone else has chosen. – Paul Wagland Dec 24 '16 at 7:19"_ – Mark Rotteveel Jan 20 '19 at 09:27

1 Answers1

2

There is no convention of using specifically com for the first segment of package names. Indeed, the Java standard library itself does not do this. Even if you limit your consideration to packages produced by third-party organizations that do have TLDs, these do not always end in .com. Of particular significance, in fact, are .org TLDs, such as the Apache project's.

Overall,

  • do choose a distinctive package name. It does not necessarily have to have more than one segment.
  • If you are not going to base it on a reversed domain name, then do not start it with com, org, or anything else that might collide or otherwise be confused with a reversed domain name.
  • Having chosen a package name, do arrange your sources in the conventional layout with respect to that name. Following that layout is not strictly necessary, but it will save you a lot of grief. A lot of tools expect that layout or at least work best with it.
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Apparently I've reached my daily vote limit and can't actually upvote this right now, interesting. That's never happened before. – temporary_user_name Jan 19 '19 at 17:42
  • Sun (then) and Oracle (now) still hold to the convention that packages should be prefixed with the reversed domain name (see also [JLS Java 11, section 6.1](https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.1) under "Package Names and Module Names"). The fact the `java.*` and `javax.*` packages don't follow that convention is an exception to the rule because they define the standard Java and Java EE packages. It is not a lease to break with convention. – Mark Rotteveel Jan 20 '19 at 09:32
  • @MarkRotteveel, I said that the convention is not *specifically to use `com`* as the first segment of a package name. And it isn't, neither according to the JLS nor to common practice, and it is relevant that the standard library provides counterexamples. The convention described in the JLS demands that you have a TLD upon which to base package names, and the question boils down to what to do if you don't in fact have a TLD to use. I have answered, in effect, to observe the purpose served by the convention: avoiding package name collisions. – John Bollinger Jan 20 '19 at 15:35
  • I agree, and I upvoted your answer, but I posted my comment because someone could misread that part of your answer as if it a 'free-for-all' to just choose any name simply because Java itself does it as well, while Java is an intentional exception to that rule (the convention to use reversed domain). – Mark Rotteveel Jan 21 '19 at 12:48