{"record":{"id":"0cd0a8cc57006a43","repo":"java-native-access/jna","slug":"jna-temporary-directory-jnatmp-does-not-exist","errorCode":null,"errorMessage":"JNA temporary directory 'jnatmp' does not exist","messagePattern":"JNA temporary directory 'jnatmp' does not exist","errorType":"exception","errorClass":"java.io.IOException","httpStatus":null,"severity":"critical","filePath":"src/com/sun/jna/Native.java","lineNumber":1422,"sourceCode":"                    xdgCacheFile = new File(System.getProperty(\"user.home\"), \".cache\");\n                } else {\n                    xdgCacheFile = new File(xdgCacheEnvironment);\n                }\n                jnatmp = new File(xdgCacheFile, \"JNA/temp\");\n            } else {\n                // Loading DLLs via System.load() under a directory with a unicode\n                // name will fail on windows, so use a hash code of the user's\n                // name in case the user's name contains non-ASCII characters\n                jnatmp = new File(tmp, \"jna-\" + System.getProperty(\"user.name\").hashCode());\n            }\n\n            jnatmp.mkdirs();\n            if (!jnatmp.exists() || !jnatmp.canWrite()) {\n                jnatmp = tmp;\n            }\n        }\n        if (!jnatmp.exists()) {\n            throw new IOException(\"JNA temporary directory '\" + jnatmp + \"' does not exist\");\n        }\n        if (!jnatmp.canWrite()) {\n            throw new IOException(\"JNA temporary directory '\" + jnatmp + \"' is not writable\");\n        }\n        return jnatmp;\n    }\n\n    /** Remove all marked temporary files in the given directory. */\n    static void removeTemporaryFiles() throws IOException {\n        File dir = getTempDir();\n        FilenameFilter filter = new FilenameFilter() {\n            @Override\n            public boolean accept(File dir, String name) {\n                return name.endsWith(\".x\") && name.startsWith(JNA_TMPLIB_PREFIX);\n            }\n        };\n        File[] files = dir.listFiles(filter);\n        for (int i=0;files != null && i < files.length;i++) {","sourceCodeStart":1404,"sourceCodeEnd":1440,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L1404-L1440","documentation":"JNA extracts its bundled native library to a temporary directory ('jnatmp' under java.io.tmpdir by default, or jna.tmpdir). Before writing, it verifies the chosen directory exists; if mkdirs() failed and no usable fallback exists, it throws this IOException. This means JNA cannot even stage the native library file, so loading will fail.","triggerScenarios":"Calling Native.load/loadLibrary (or any first JNA native call) when the configured temp directory (jna.tmpdir or jnatmp* under java.io.tmpdir) does not exist and could not be created — e.g. parent dir missing, read-only filesystem, or disk full.","commonSituations":"Containers/app servers with read-only /tmp, java.io.tmpdir pointing to a deleted or never-created path, security policies blocking file creation, or tmpwatch/systemd-tmpfiles cleaning the directory mid-run.","solutions":["Pre-create the temp directory and make it writable: mkdir -p $JNA_TMPDIR with proper permissions.","Set -Djna.tmpdir=/some/writable/dir (or JNA_TMPDIR env var) to a directory that exists and is writable by the JVM user.","Restore java.io.tmpdir to an existing writable location (or restart with -Djava.io.tmpdir=/writable/tmp).","Fix the environment: remount the filesystem read-write, free disk space, or adjust SecurityManager/policy rules blocking mkdirs."],"exampleFix":"// before (read-only /tmp, no override)\njava -jar app.jar // JNA tries /tmp/jnatmp... -> IOException\n\n// after\nmkdir -p /var/tmp/jna && chmod 777 /var/tmp/jna\njava -Djna.tmpdir=/var/tmp/jna -jar app.jar","handlingStrategy":"validation","validationCode":"File dir = new File(System.getProperty(\"jna.tmpdir\",\n    System.getProperty(\"java.io.tmpdir\")));\nif (!dir.isDirectory() && !dir.mkdirs()) {\n    throw new IllegalStateException(\"Create a writable jna.tmpdir before loading JNA libraries\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    MyLib lib = Native.load(\"mylib\", MyLib.class);\n} catch (IOException e) {\n    if (e.getMessage().contains(\"does not exist\")) {\n        System.setProperty(\"jna.tmpdir\", \"/var/tmp/jna\");\n        new File(\"/var/tmp/jna\").mkdirs();\n        // retry load in a fresh classloader/restart\n    } else { throw e; }\n}","preventionTips":["Provision and pre-create jna.tmpdir in container images and startup scripts.","Set -Djna.tmpdir explicitly in read-only /tmp environments.","Exclude JNA temp dirs from tmp cleaners (tmpwatch/systemd-tmpfiles).","Smoke-test the first native load in CI with the same filesystem permissions as production."],"tags":["jna","temp-directory","io","filesystem"],"backgroundTag":"directory-not-found","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}