2

Note that I'm not asking about Java package naming conventions.

For example, Java does not legally allow package names to start with a number.

However, the official Java docs doesn't seem to cover the actual rules governing what can or can't be used in a package name.

What are the set of legal package names?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Him
  • 5,257
  • 3
  • 26
  • 83

2 Answers2

6

It is a Java identifier, followed by N (periods+identifier)s.

PackageDeclaration:
{PackageModifier} package Identifier {. Identifier} ;

https://docs.oracle.com/javase/specs/jls/se16/html/jls-7.html#jls-7.4

An identifier is (incl. some nested definitions):

Identifier:
IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:
JavaLetter {JavaLetterOrDigit}

JavaLetter:
any Unicode character that is a "Java letter"

JavaLetterOrDigit:
any Unicode character that is a "Java letter-or-digit"

A "Java letter" is a character for which the method Character.isJavaIdentifierStart(int) returns true.

A "Java letter-or-digit" is a character for which the method Character.isJavaIdentifierPart(int) returns true.

https://docs.oracle.com/javase/specs/jls/se16/html/jls-3.html#jls-Identifier

Michael
  • 41,989
  • 11
  • 82
  • 128
  • Oh, good grief. I just now realized that `$` is a valid package name. – chrylis -cautiouslyoptimistic- Apr 30 '21 at 15:08
  • 1
    @chrylis-cautiouslyoptimistic- The number of characters which are allowed absolutely dwarfs the characters that are expressly prohibited. They really only restrict a few obviously stupid choices that could be typed with a QWERTY keyboard. Unicode is massive. Subscripts are allowed, every Chinese and Japanese character is allowed (understandably when you think about it, but still might not be obvious to westerners), the copyright symbol is allowed, as are characters which are for intents and purposes non-printable. Java obfuscators I've seen are really not creative enough given their options – Michael Apr 30 '21 at 15:36
3

The documentation you linked is the official Java tutorial, not the actual technical documentation of Java. You can find the informaton you need in the JLS here: https://docs.oracle.com/javase/specs/jls/se16/html/jls-7.html#jls-7.4.1

According to the JLS, the syntax for a package name has the form Identifier {. Identifier}. It's a list of legal identifiers (not keywords, null, true/false etc) which are separated by dots.

marstran
  • 26,413
  • 5
  • 61
  • 67