How to * really * ensure that Java package names are unique?

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

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

  • You cannot (or cannot afford) to own a domain
  • Still create package names, hoping they will be unique.

This only introduces the scope of packet conflicts.

Next, suppose I have [mycompany] .com. What prevents a developer from creating a library with the same package prefix as mine and distributing it? AFAIK, there is no legal binding to package names ("you MUST own the domain that you used in your Java package"). Not to mention that this action on the developer part may not be intentional (how many of us comb the web page to find a non-existent domain name before we name our packages?).

Also, even if I have a domain name, it may or may not be suitable for use as a package name (my domain name may include hyphens, or it may be so long that, although this is the name of a legal package, it can still make it impractical).

So my question is: how can I make a unique Java package name?

+6
source share
2 answers

You cannot absolutely guarantee that your package names will be globally unique. As you already mentioned, nothing prevents anyone from using the identical package name, regardless of who owns what the package name is based on. There is no authoritative index of package names used.

+14
source

I see two answers to your problem:

  • Make a SourceForge student project design and net.sf.myproject package and name packages (or use any similar free host like GitHub ). This package name will probably not be used by others, although there is no guarantee, as Matt Hearn points out .
  • In addition, if you cannot or do not want to afford a domain, your program is unlikely to be used by a wide audience in any case, and you can call your packages what you want.
+7
source

Source: https://habr.com/ru/post/916358/


All Articles