{"record":{"id":"c65f8f912264a7ee","repo":"JetBrains/intellij-community","slug":"this-template-did-not-produce-a-java-class-or-an-i","errorCode":null,"errorMessage":"This template did not produce a Java class or an interface\\n","messagePattern":"This template did not produce a Java class or an interface\\\\n","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java","lineNumber":57,"sourceCode":"  }\n\n  private static PsiClass createClassOrInterface(Project project,\n                                                 PsiDirectory directory,\n                                                 String content,\n                                                 boolean reformat,\n                                                 String extension, @Nullable String optionalClassName) throws IncorrectOperationException {\n    if (extension == null) extension = JavaFileType.INSTANCE.getDefaultExtension();\n    final String name = \"myClass\" + \".\" + extension;\n    final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(name, JavaLanguage.INSTANCE, content, false, false);\n    LanguageLevel highest = LanguageLevel.HIGHEST;\n    LanguageLevel implicitClassesMinimumLevel = JavaFeature.IMPLICIT_CLASSES.getStandardLevel();\n    if (highest.isLessThan(implicitClassesMinimumLevel)) {\n      highest = implicitClassesMinimumLevel;\n    }\n    psiFile.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, highest);\n\n    if (!(psiFile instanceof PsiJavaFile psiJavaFile)){\n      throw new IncorrectOperationException(\"This template did not produce a Java class or an interface\\n\"+psiFile.getText());\n    }\n    final PsiClass[] classes = psiJavaFile.getClasses();\n    if (classes.length == 0) {\n      throw new IncorrectOperationException(\"This template did not produce a Java class or an interface\\n\"+psiFile.getText());\n    }\n    PsiClass createdClass = classes[0];\n    String className;\n    if (optionalClassName != null && createdClass instanceof PsiImplicitClass) {\n      className = optionalClassName;\n    }\n    else {\n      className = createdClass.getName();\n    }\n    JavaDirectoryServiceImpl.checkCreateClassOrInterface(directory, className);\n\n    final LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);\n    if (ll.compareTo(LanguageLevel.JDK_1_5) < 0) {\n      if (createdClass.isAnnotationType()) {","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java#L39-L75","documentation":"createFromTemplate parses the expanded file-template content by creating a PSI file in Java language; if that PSI file is not a PsiJavaFile at all, an IncorrectOperationException with the full expanded text appended is thrown. It means the template output could not be interpreted as Java source — e.g. the template produced JSON, XML, plain text, or the file template was invoked with the wrong handler for its content.","triggerScenarios":"Invoking 'New | Java Class' (or another Java file template) where the template body renders to non-Java content: a template edited to hold arbitrary text, a file-type mix-up so content is parsed as a different language, or a custom template named like a Java one but containing non-Java syntax.","commonSituations":"Users editing built-in class templates and accidentally breaking them; third-party plugins registering templates that route through JavaCreateFromTemplateHandler; template property expansion failing so only literal non-Java text remains.","solutions":["Open Settings | Editor | File and Code Templates and check the offending template body renders valid Java (class/interface/enum declaration).","Verify you are using the right template type: non-Java templates must not be created through the Java 'New Class' action.","Use 'Go to definition'/preview on the template to inspect the expanded output; the exception message includes the exact text that failed to parse."],"exampleFix":"// before (template body that yields no Java type)\n{ \"note\": \"${NAME}\" }\n\n// after\n#if (${PACKAGE_NAME} != \"\")package ${PACKAGE_NAME};#end\nclass ${NAME} {\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  PsiClass c = JavaCreateFromTemplateHandler.createFromTemplate(project, directory, name, template, props);\n} catch (IncorrectOperationException e) {\n  // message contains the expanded template text — inspect it to fix the template body\n  showError(e.getMessage());\n}","preventionTips":["Keep a type declaration in every Java file template","Preview templates in Settings | Editor | File and Code Templates before use","Do not route non-Java templates through the Java class creation action"],"tags":["file-templates","java","psi","ide"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}