{"record":{"id":"0215b43947f40d18","repo":"openjdk/jdk","slug":"cannot-create-directory","errorCode":null,"errorMessage":"Cannot create directory","messagePattern":"Cannot create directory","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"warning","filePath":"src/demo/share/jfc/FileChooserDemo/ExampleFileSystemView.java","lineNumber":67,"sourceCode":" * You can provide a superclass of the FileSystemView class with your own functionality.\n *\n * @author Pavel Porvatov\n */\npublic class ExampleFileSystemView extends FileSystemView {\n\n    /**\n     * Creates a new folder with the default name \"New folder\". This method is invoked\n     * when the user presses the \"New folder\" button.\n     */\n    public File createNewFolder(File containingDir) throws IOException {\n        File result = new File(containingDir, \"New folder\");\n\n        if (result.exists()) {\n            throw new IOException(\"Directory 'New folder' exists\");\n        }\n\n        if (!result.mkdir()) {\n            throw new IOException(\"Cannot create directory\");\n        }\n\n        return result;\n    }\n\n    /**\n     * Returns a list which appears in a drop-down list of the FileChooser component.\n     * In this implementation only the home directory is returned.\n     */\n    @Override\n    public File[] getRoots() {\n        return new File[] { getHomeDirectory() };\n    }\n\n    /**\n     * Returns a string that represents a directory or a file in the FileChooser component.\n     * A string with all upper case letters is returned for a directory.\n     * A string with all lower case letters is returned for a file.","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/src/demo/share/jfc/FileChooserDemo/ExampleFileSystemView.java#L49-L85","documentation":"IOException from ExampleFileSystemView.createNewFolder when File.mkdir() returns false — the OS refused to create the 'New folder' directory. mkdir fails without throwing, so the demo converts the boolean into an IOException, losing the OS error detail.","triggerScenarios":"containingDir is read-only, does not exist, is a file, or the filesystem denies mkdir (permissions, immutable attribute, sandbox).","commonSituations":"User browsing into a read-only mount or system directory in the demo; disk full; macOS/Windows protected directories; applications reusing the demo view against restricted filesystems.","solutions":["Choose a writable parent directory before creating a folder.","Check containingDir.isDirectory() && containingDir.canWrite() beforehand.","In adapted code, use java.nio.Files.createDirectory which reports the real errno."],"exampleFix":"// before\nif (!result.mkdir()) throw new IOException(\"Cannot create directory\");\n\n// after: real error surfaced\ntry {\n    Files.createDirectory(result.toPath());\n} catch (IOException e) {\n    throw new IOException(\"Cannot create \" + result + \": \" + e.getMessage(), e);\n}","handlingStrategy":"validation","validationCode":"// verify the parent is a writable directory before mkdir\nif (containingDir == null || !containingDir.isDirectory() || !containingDir.canWrite())\n    throw new IOException(\"cannot create folder in \" + containingDir);","typeGuard":null,"tryCatchPattern":"try {\n    Files.createDirectory(result.toPath());\n} catch (FileAlreadyExistsException e) {\n    // pick another name\n} catch (AccessDeniedException e) {\n    // tell the user the directory is read-only\n} catch (IOException e) {\n    // surface the real errno-based message\n}","preventionTips":["Prefer java.nio.file.Files.createDirectory over File.mkdir — it reports the actual cause.","Check canWrite()/isDirectory() on the parent before offering the 'New folder' action in the UI."],"tags":["swing","filechooser","filesystem","permissions","demo"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}