java-native-access/jna · error · java.io.IOException
Failed to create temporary file for name library: e.getMessa
Error message
Failed to create temporary file for name library: e.getMessage()
What it means
While unpacking the native library resource to a temporary file, any IOException encountered (creating the temp file or writing the buffered stream) is wrapped as IOException 'Failed to create temporary file for <name> library: <reason>'. The native library could not be materialized on disk, so loading cannot proceed.
Source
Thrown at src/com/sun/jna/Native.java:1252
try {
// Suffix is required on windows, or library fails to load
// Let Java pick the suffix, except on windows, to avoid
// problems with Web Start.
File dir = getTempDir();
lib = File.createTempFile(JNA_TMPLIB_PREFIX, Platform.isWindows()?".dll":null, dir);
if (!Boolean.getBoolean("jnidispatch.preserve")) {
lib.deleteOnExit();
}
LOG.log(DEBUG, "Extracting library to {0}", lib.getAbsolutePath());
fos = new FileOutputStream(lib);
int count;
byte[] buf = new byte[1024];
while ((count = is.read(buf, 0, buf.length)) > 0) {
fos.write(buf, 0, count);
}
}
catch(IOException e) {
throw new IOException("Failed to create temporary file for " + name + " library: " + e.getMessage());
}
finally {
try { is.close(); } catch(IOException e) { }
if (fos != null) {
try { fos.close(); } catch(IOException e) { }
}
}
}
return lib;
}
/**
* Initialize field and method IDs for native methods of this class.
* Returns the size of a native pointer.
**/
private static native int sizeof(int type);
private static native String getNativeVersion();View on GitHub (pinned to d036ad9781)
Solutions
- Set -Djna.tmpdir (or java.io.tmpdir) to a writable, executable directory and retry
- Free disk space / raise quota on the temp filesystem
- If the library is already unpacked on disk, set -Djna.nounpack=true and point jna.boot.library.path at it
- Check the embedded reason in the message (permission vs space) and fix filesystem permissions accordingly
Example fix
// before java -jar app.jar # tmpdir read-only // after java -Djna.tmpdir=/var/lib/app/tmp -jar app.toml -> ensure /var/lib/app/tmp is writable and mounted with exec
Defensive patterns
Strategy: validation
Validate before calling
// ensure temp dir is writable before first JNA call
File tmp = new File(System.getProperty("jna.tmpdir", System.getProperty("java.io.tmpdir")));
if (!tmp.canWrite()) throw new IllegalStateException("JNA temp dir not writable: " + tmp);
if (tmp.getUsableSpace() < 10L * 1024 * 1024) throw new IllegalStateException("Low space in JNA temp dir: " + tmp); Try / catch
try { Native.load("c", CLibrary.class); } catch (UnsatisfiedLinkError e) { Throwable c = e; while ((c = c.getCause()) != null) { if (c instanceof IOException && String.valueOf(c.getMessage()).contains("Failed to create temporary file")) { throw new IllegalStateException("Fix jna.tmpdir permissions/space", e); } } throw e; } Prevention
- Set -Djna.tmpdir to a dedicated writable directory with exec permission
- Monitor temp filesystem space in containers
- Use -Djna.nounpack with a pre-installed native library in restricted environments
When it happens
Trigger: extractFromResourcePath stream-copy loop failing because the temp directory is read-only, full, or File.createTempFile failed; disk quota exceeded; antivirus/permissions blocking file creation in java.io.tmpdir.
Common situations: Containers with read-only /tmp; tmpwatch cleaner races; small tmpfs; corporate policy denying execute from temp; running as a user without write access to the temp dir.
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- Can't obtain InputStream for resourcePath
- No trash location found (define fileutils.trash to be the pa
- UnsatisfiedLinkError(e.getMessage())
- Native library (resourcePath) not found in resource path (pa
- File URL url could not be properly decoded
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/473215a92424d5c6.
Report an issue: GitHub.