{"record":{"id":"ba3b0176272e8ac9","repo":"languagetool-org/languagetool","slug":"dir-getabsolutepath-is-not-a-directory-cannot","errorCode":null,"errorMessage":"<dir.getAbsolutePath()> is not a directory, cannot use recursion","messagePattern":"<dir\\.getAbsolutePath\\(\\)> is not a directory, cannot use recursion","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"languagetool-commandline/src/main/java/org/languagetool/commandline/Main.java","lineNumber":315,"sourceCode":"        ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE,\n        ByteOrderMark.UTF_32BE,ByteOrderMark.UTF_32LE);\n      if (bomIn.hasBOM() && encoding == null) {\n        charsetName = bomIn.getBOMCharsetName();\n      }\n      is = bomIn;\n    }\n    return new InputStreamReader(new BufferedInputStream(is), charsetName);\n  }\n\n  private boolean isStdIn(String filename) {\n    return \"-\".equals(filename);\n  }\n\n  private void runRecursive(String filename, String encoding, boolean xmlFiltering, JLanguageTool.Level level) {\n    File dir = new File(filename);\n    File[] files = dir.listFiles();\n    if (files == null) {\n      throw new IllegalArgumentException(dir.getAbsolutePath() + \" is not a directory, cannot use recursion\");\n    }\n    for (File file : files) {\n      try {\n        if (file.isDirectory()) {\n          runRecursive(file.getAbsolutePath(), encoding, xmlFiltering, level);\n        } else {\n          if (options.isLineByLine()) {\n            runOnFileLineByLine(file.getAbsolutePath(), encoding, level);\n          } else {\n            runOnFile(file.getAbsolutePath(), encoding, xmlFiltering);\n          }\n        }\n      } catch (Exception e) {\n        throw new RuntimeException(\"Could not check text in file \" + file, e);\n      }\n    }    \n  }\n","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-commandline/src/main/java/org/languagetool/commandline/Main.java#L297-L333","documentation":"Main.runRecursive recurses into a directory to process every file. File.listFiles() returns null when the path is not a directory (or is unreadable), and the method converts that into IllegalArgumentException '<path> is not a directory, cannot use recursion'. This is the recursion-mode (-r/--recursive) guard.","triggerScenarios":"Passing -r/--recursive together with a plain file (or a nonexistent path) instead of a directory, e.g. `languagetool -r -l en file.txt`; also directories the process cannot read cause listFiles() to return null and the same error.","commonSituations":"Enabling recursion in a script that sometimes receives single files; path typos so the directory does not exist; permission problems on the directory.","solutions":["Pass a directory (not a file) when using -r/--recursive","Verify the path exists and is readable before running (e.g. `[ -d \"$DIR\" ]` or File.isDirectory())","Drop -r for single-file input","Fix filesystem permissions if the directory cannot be read"],"exampleFix":"// before\nltMain.runRecursive(\"notes.txt\", \"utf-8\", false, null);\n// after\nFile dir = new File(\"notes/\");\nif (dir.isDirectory()) {\n  ltMain.runRecursive(dir.getAbsolutePath(), \"utf-8\", false, null);\n}","handlingStrategy":"validation","validationCode":"File dir = new File(path);\nif (!dir.isDirectory()) {\n  throw new IllegalArgumentException(path + \" must be an existing, readable directory when using --recursive\");\n}","typeGuard":"static boolean isReadableDirectory(String path) {\n  File d = new File(path);\n  return d.isDirectory() && d.canRead();\n}","tryCatchPattern":"try {\n  runRecursive(path, encoding, xmlFiltering, level);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().endsWith(\"is not a directory, cannot use recursion\")) {\n    System.err.println(\"--recursive requires a directory; got: \" + path);\n    System.exit(2);\n  }\n  throw e;\n}","preventionTips":["Check File.isDirectory() (or [ -d \"$DIR\" ]) before enabling -r/--recursive","Use -r only for directory inputs; omit it for single files","Verify read permissions on the target directory","Validate paths exist to catch typos before the run"],"tags":["cli","commandline","filesystem","directory","recursion"],"backgroundTag":"path-is-not-a-directory","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}