7

The standard way to avoid package name conflicts in Java is to use reverse-domain convention com.[mycompany].[rest-of-the-package-name]. This works brilliantly ... if one owns the domain [mycompany].com.

However, there are several individual developers (or students) who:

  • Don't (or can't afford to) own a domain
  • Still conjure up some package names hoping they will be unique.

This alone introduces scope for package conflicts.

Further, assume that I own [mycompany].com. What is preventing a developer from creating a library with the same package prefix as mine and distributing it? AFAIK, there is no legal binding with regard to package names ("you MUST own the domain which you have used in you Java package"). Not to mention that this action on the developer's part might not be intentional (how many of us comb the web looking for a non-existent domain name before we name our packages?).

Even further, even if I own a domain name, it may or may not be suitable for use as a package name (my domain name could include hyphens, or it could be so long that although it is a legal package name, it could still make it impractical).

So my question is: How do I really make my Java package name unique?

curioustechizen
  • 10,572
  • 10
  • 61
  • 110
  • 2
    Worry less about package names and more about good software practices. You can always rename packages! – Nick May 22 '12 at 13:53
  • @Jivings: yes, they are, but how would you prevent others from using your email address in a package name? – Vlad May 22 '12 at 13:54
  • 5
    you can use (afford) a github.com account and it's also a good way for others to find your package `com.github.username.package` – bjarneh May 22 '12 at 13:54
  • @Vlad Was in response to the student issue. – Jivings May 22 '12 at 13:55
  • @Vlad how do you prevent people from using your domain in a package name? Ooooh.. Yeah... You really can't. – Krrose27 May 22 '12 at 13:58
  • A bit late but as nobody mentioned it, to have a truly unique package name you could use a uuid as the package name. Not very nice in practise, but it would be a way to have a truly 'unique' package name. – Pharap Jan 09 '15 at 01:21

2 Answers2

15

You cannot absolutely guarantee that your package names will be globally unique. As you mentioned, there's nothing stopping someone else from using an identical package name, regardless of who owns whatever it is the package name is based on. There's no authoritative index of package names in use.

Matt
  • 2,187
  • 1
  • 16
  • 23
7

I see two answers to your problem:

  • Make your student project a SourceForge project and name packages net.sf.myproject (or use any similar free host, such as GitHub). Such a package name is likely not to be used by others, although there is no guarantee as Matt Hurne points out
  • Other than that, if you can't or don't want to afford a domain, your program is unlikely to be used by a wide audience anyway, and you can name your packages whatever you want
Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509