{"record":{"id":"a908c44c381d0364","repo":"JetBrains/intellij-community","slug":"cannot-create-class-in-invalid-package-qualifie","errorCode":null,"errorMessage":"Cannot create class in invalid package: '{qualifiedName}'","messagePattern":"Cannot create class in invalid package: '(.+?)'","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-impl/src/com/intellij/psi/impl/file/JavaDirectoryServiceImpl.java","lineNumber":215,"sourceCode":"                             FileTemplateManager.getInstance(project).internalTemplateToSubject(templateName), templateName);\n  }\n\n  @Override\n  public void checkCreateClass(@NotNull PsiDirectory dir, @NotNull String name) throws IncorrectOperationException {\n    checkCreateClassOrInterface(dir, name);\n  }\n\n  public static void checkCreateClassOrInterface(@NotNull PsiDirectory directory, String name) throws IncorrectOperationException {\n    PsiUtil.checkIsIdentifier(directory.getManager(), name);\n\n    String fileName = name + \".\" + JavaFileType.INSTANCE.getDefaultExtension();\n    directory.checkCreateFile(fileName);\n\n    PsiNameHelper helper = PsiNameHelper.getInstance(directory.getProject());\n    PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);\n    String qualifiedName = aPackage == null ? null : aPackage.getQualifiedName();\n    if (!StringUtil.isEmpty(qualifiedName) && !helper.isQualifiedName(qualifiedName)) {\n      throw new IncorrectOperationException(\"Cannot create class in invalid package: '\"+qualifiedName+\"'\");\n    }\n  }\n\n  @Override\n  public boolean isSourceRoot(@NotNull PsiDirectory dir) {\n    final VirtualFile file = dir.getVirtualFile();\n    final VirtualFile sourceRoot = ProjectRootManager.getInstance(dir.getProject()).getFileIndex().getSourceRootForFile(file);\n    return file.equals(sourceRoot);\n  }\n\n  @Override\n  public LanguageLevel getLanguageLevel(@NotNull PsiDirectory dir) {\n    return JavaPsiImplementationHelper.getInstance(dir.getProject()).getEffectiveLanguageLevel(dir.getVirtualFile());\n  }\n\n}\n","sourceCodeStart":197,"sourceCodeEnd":232,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl/src/com/intellij/psi/impl/file/JavaDirectoryServiceImpl.java#L197-L232","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","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."],"exampleFix":"// before: source root layout\nsrc/my-pkg/Main.java   // package my-pkg; -> invalid\n// after\nsrc/mypkg/Main.java    // package mypkg;","handlingStrategy":"validation","validationCode":"PsiPackage pkg = JavaDirectoryService.getInstance().getPackage(directory);\nString qname = pkg == null ? null : pkg.getQualifiedName();\nif (qname != null && !qname.isEmpty() && !PsiNameHelper.getInstance(project).isQualifiedName(qname)) {\n  // invalid package directory; do not attempt class creation\n}","typeGuard":null,"tryCatchPattern":"try { JavaDirectoryServiceImpl.checkCreateClassOrInterface(dir, name); } catch (IncorrectOperationException e) { /* directory is in an invalid package; prompt user to fix source layout */ }","preventionTips":["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."],"tags":["java","package-name","create-class","source-root"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}