{"record":{"id":"c797032b81c95ff5","repo":"JetBrains/intellij-community","slug":"the-file-was-expected-to-be-of-java-file-type","errorCode":null,"errorMessage":"The file '{}' was expected to be of JAVA file type, but got: '{}'","messagePattern":"The file '(.+?)' was expected to be of JAVA file type, but got: '(.+?)'","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java","lineNumber":96,"sourceCode":"\n      if (createdClass.isEnum()) {\n        throw new IncorrectOperationException(\"Enums only supported at language level 1.5 and higher\");\n      }\n    }\n\n    psiJavaFile = (PsiJavaFile)psiJavaFile.setName(className + \".\" + extension);\n    PsiElement addedElement = directory.add(psiJavaFile);\n    if (addedElement instanceof PsiJavaFile) {\n      psiJavaFile = (PsiJavaFile)addedElement;\n      if(reformat){\n        CodeStyleManager.getInstance(project).scheduleReformatWhenSettingsComputed(psiJavaFile);\n      }\n\n      return psiJavaFile.getClasses()[0];\n    }\n    else {\n      PsiFile containingFile = addedElement.getContainingFile();\n      throw new IncorrectOperationException(\"The file '\" + containingFile +  \"' was expected to be of JAVA file type, but got: '\"+ containingFile.getFileType() + \"'\");\n    }\n  }\n\n  static void hackAwayEmptyPackage(PsiJavaFile file, FileTemplate template, Map<String, Object> props) throws IncorrectOperationException {\n    if (!template.isTemplateOfType(JavaFileType.INSTANCE)) return;\n\n    String packageName = (String)props.get(FileTemplate.ATTRIBUTE_PACKAGE_NAME);\n    if(packageName == null || packageName.isEmpty() || packageName.equals(FileTemplate.ATTRIBUTE_PACKAGE_NAME)){\n      PsiPackageStatement packageStatement = file.getPackageStatement();\n      if (packageStatement != null) {\n        packageStatement.delete();\n      }\n    }\n  }\n\n  @Override\n  public boolean handlesTemplate(@NotNull FileTemplate template) {\n    FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension());","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java#L78-L114","documentation":"After adding the generated file to the target directory, the added element is expected to be a PsiJavaFile; if the containing file's type resolves to something other than the JAVA file type, an IncorrectOperationException names the file and its actual file type. The element was created with a '.java' extension, so this means IntelliJ's file-type resolution mapped that name to a different type — usually because a custom/other plugin registered a pattern or extension collision that wins over JAVA.","triggerScenarios":"directory.add(psiJavaFile) returns an element whose file type is not JavaFileType — e.g. a plugin registered '*.java' (or an overlapping filename pattern) under a different file type, or a corrupted file-type association set.","commonSituations":"Third-party plugins claiming the .java extension; manually edited 'file types' configuration in Settings | Editor | File Types; importing settings from another machine with conflicting patterns.","solutions":["Open Settings | Editor | File Types, find the non-Java type that lists '*.java' or the colliding pattern, and remove that pattern so it lives only under Java.","Disable suspect plugins one by one (especially language/template plugins registering file patterns) and retry file creation.","As a last resort, restore default file-type associations (delete the 'filetypes' chunk in config or use 'Restore Defaults' on the affected type)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { directory.add(psiJavaFile); } catch (IncorrectOperationException e) { if (e.getMessage().contains(\"expected to be of JAVA file type\")) { fixFileTypeAssociations(); } throw e; }","preventionTips":["Audit Settings | Editor | File Types for '*.java' registered under non-Java types","Be cautious with plugins that claim broad extension patterns","Export/inspect the filetypes config after importing settings from other machines"],"tags":["file-types","java","psi","ide","plugin-conflict"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}