JetBrains/intellij-community · error · IncorrectOperationException
Cannot create class in invalid package: '{qualifiedName}'
Error message
Cannot create class in invalid package: '{qualifiedName}' What it means
Thrown by JavaDirectoryServiceImpl.checkCreateClassOrInterface() when the directory's package qualified name is non-empty but not a valid Java qualified name (PsiNameHelper.isQualifiedName fails). The directory itself exists in a package whose name is malformed, so no class can be created in it. Note the name identifier and file-creation checks have already passed at this point.
Source
Thrown at java/java-impl/src/com/intellij/psi/impl/file/JavaDirectoryServiceImpl.java:215
FileTemplateManager.getInstance(project).internalTemplateToSubject(templateName), templateName);
}
@Override
public void checkCreateClass(@NotNull PsiDirectory dir, @NotNull String name) throws IncorrectOperationException {
checkCreateClassOrInterface(dir, name);
}
public static void checkCreateClassOrInterface(@NotNull PsiDirectory directory, String name) throws IncorrectOperationException {
PsiUtil.checkIsIdentifier(directory.getManager(), name);
String fileName = name + "." + JavaFileType.INSTANCE.getDefaultExtension();
directory.checkCreateFile(fileName);
PsiNameHelper helper = PsiNameHelper.getInstance(directory.getProject());
PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
String qualifiedName = aPackage == null ? null : aPackage.getQualifiedName();
if (!StringUtil.isEmpty(qualifiedName) && !helper.isQualifiedName(qualifiedName)) {
throw new IncorrectOperationException("Cannot create class in invalid package: '"+qualifiedName+"'");
}
}
@Override
public boolean isSourceRoot(@NotNull PsiDirectory dir) {
final VirtualFile file = dir.getVirtualFile();
final VirtualFile sourceRoot = ProjectRootManager.getInstance(dir.getProject()).getFileIndex().getSourceRootForFile(file);
return file.equals(sourceRoot);
}
@Override
public LanguageLevel getLanguageLevel(@NotNull PsiDirectory dir) {
return JavaPsiImplementationHelper.getInstance(dir.getProject()).getEffectiveLanguageLevel(dir.getVirtualFile());
}
}
View on GitHub (pinned to be881553f2)
Solutions
- Rename the offending source directory (and any parent segments) so every segment is a valid Java identifier: letters, digits, $, _, no hyphens/spaces/dots.
- Check the module's source roots (Project Structure > Modules > Sources) and unmark directories that are not real packages (e.g. resources, generated non-Java output).
- If the folder name is fixed by tooling, move the Java code into a properly named package directory and adjust the package statement.
- Use PsiNameHelper.getInstance(project).isQualifiedName(name) on the computed package name before attempting creation.
Example fix
// before: source root layout src/my-pkg/Main.java // package my-pkg; -> invalid // after src/mypkg/Main.java // package mypkg;
Defensive patterns
Strategy: validation
Validate before calling
PsiPackage pkg = JavaDirectoryService.getInstance().getPackage(directory);
String qname = pkg == null ? null : pkg.getQualifiedName();
if (qname != null && !qname.isEmpty() && !PsiNameHelper.getInstance(project).isQualifiedName(qname)) {
// invalid package directory; do not attempt class creation
} Try / catch
try { JavaDirectoryServiceImpl.checkCreateClassOrInterface(dir, name); } catch (IncorrectOperationException e) { /* directory is in an invalid package; prompt user to fix source layout */ } Prevention
- Keep every directory under a source root a valid Java identifier segment (no hyphens, spaces, dots).
- Review marked source roots after importing projects; unmark resource/generated folders.
- Run the 'Package name does not correspond to the file path' style inspections periodically.
When it happens
Trigger: Calling createClass/checkCreateClassOrInterface on a PsiDirectory whose package name contains illegal characters or segments: a source folder named 'my-pkg', a directory with spaces or keywords like 'int'/'class' as segment names, or nested directories forming 'com..x'.
Common situations: Importing a project where source roots contain folders that are not valid Java identifiers (hyphens, dots in folder names, leading digits); resources accidentally marked as sources; multi-release or generated directories with odd names; packages named with Java keywords.
Related errors
- {0} is not a valid package name
- Cannot create {0} - incorrect {1} template.
- Cannot create class-file
- Cannot create package '{0}' in source folder {1}
- Invalid package name: {0}
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/a908c44c381d0364.
Report an issue: GitHub.