JetBrains/intellij-community · error · OperationFailedException

Invalid package name: {0}

Error message

Invalid package name: {0}

What it means

JavaExtractSuperBaseDialog.preparePackage throws OperationFailedException when the target package name typed into the Extract Superclass/Extract Interface (extract base) dialog is not a valid Java qualified name. PsiNameHelper.isQualifiedName must accept it; the only exemption is extracting from the default package into the default package (both empty).

Source

Thrown at java/java-impl-refactorings/src/com/intellij/refactoring/extractSuperclass/JavaExtractSuperBaseDialog.java:134

        }
      }
    }
    return directories[0];
  }

  @Override
  protected boolean isPossibleToRenameOriginal() {
    //disable for anonymous classes
    return mySourceClass.getNameIdentifier() != null;
  }

  @Override
  protected void preparePackage() throws OperationFailedException {
    final String targetPackageName = getTargetPackageName();
    final PsiFile containingFile = mySourceClass.getContainingFile();
    final boolean fromDefaultPackage = containingFile instanceof PsiClassOwner && ((PsiClassOwner)containingFile).getPackageName().isEmpty();
    if (!(fromDefaultPackage && StringUtil.isEmpty(targetPackageName)) && !PsiNameHelper.getInstance(myProject).isQualifiedName(targetPackageName)) {
      throw new OperationFailedException(JavaRefactoringBundle.message("invalid.package.name", targetPackageName));
    }
    final PsiPackage aPackage = JavaPsiFacade.getInstance(myProject).findPackage(targetPackageName);
    if (aPackage != null) {
      final PsiDirectory[] directories = aPackage.getDirectories(mySourceClass.getResolveScope());
      if (directories.length >= 1) {
        myTargetDirectory = getDirUnderSameSourceRoot(directories);
      }
    }

    final MoveDestination moveDestination =
      myDestinationFolderComboBox.selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), targetPackageName), false);
    if (moveDestination == null) return;

    myTargetDirectory = myTargetDirectory != null ? WriteAction
      .compute(() -> moveDestination.getTargetDirectory(myTargetDirectory)) : null;

    if (myTargetDirectory == null) {
      throw new OperationFailedException(""); // message already reported by PackageUtil

View on GitHub (pinned to be881553f2)

Solutions

  1. Correct the package name to dot-separated valid Java identifiers, e.g. 'com.example.foo'.
  2. Remove leading/trailing dots and empty segments.
  3. Do not use Java keywords or hyphes/slashes; a segment may not start with a digit.
  4. If the source class is in the default package, leaving the target package empty is also acceptable.

Example fix

# before:
Package: com/example/foo

# after:
Package: com.example.foo
Defensive patterns

Strategy: validation

Validate before calling

boolean fromDefault = containingFile instanceof PsiClassOwner o && o.getPackageName().isEmpty();
if (!(fromDefault && targetPackageName.isEmpty())
    && !PsiNameHelper.getInstance(project).isQualifiedName(targetPackageName)) {
  // block OK with 'invalid package name' before running the dialog action
}

Try / catch

try { dialog.preparePackage(); } catch (OperationFailedException e) { /* invalid package name: correct the field */ }

Prevention

When it happens

Trigger: Entering a package like 'com..foo', '.com.foo', 'com.foo.' or a keyword-containing segment ('int.x') in the package field of the Extract Superclass dialog; also whitespace-only or illegal characters (dashes, slashes).

Common situations: Typing directory-like paths ('src/com/foo') instead of package syntax; trailing dots from autocompletion; pasting package names containing invalid characters; users new to Java package naming rules (keyword segments, digits-first segments).

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/a158d440adb242a3. Report an issue: GitHub.