{"record":{"id":"46263faaff2ad28d","repo":"java-native-access/jna","slug":"can-t-obtain-inputstream-for-resourcepath","errorCode":null,"errorMessage":"Can't obtain InputStream for resourcePath","messagePattern":"Can't obtain InputStream for resourcePath","errorType":"exception","errorClass":"java.io.IOException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Native.java","lineNumber":1230,"sourceCode":"        LOG.log(DEBUG, \"Found library resource at {0}\", url);\n\n        File lib = null;\n        if (url.getProtocol().toLowerCase().equals(\"file\")) {\n            try {\n                lib = new File(new URI(url.toString()));\n            }\n            catch(URISyntaxException e) {\n                lib = new File(url.getPath());\n            }\n            LOG.log(DEBUG, \"Looking in {0}\", lib.getAbsolutePath());\n            if (!lib.exists()) {\n                throw new IOException(\"File URL \" + url + \" could not be properly decoded\");\n            }\n        }\n        else if (!Boolean.getBoolean(\"jna.nounpack\")) {\n            InputStream is = url.openStream();\n            if (is == null) {\n                throw new IOException(\"Can't obtain InputStream for \" + resourcePath);\n            }\n\n            FileOutputStream fos = null;\n            try {\n                // Suffix is required on windows, or library fails to load\n                // Let Java pick the suffix, except on windows, to avoid\n                // problems with Web Start.\n                File dir = getTempDir();\n                lib = File.createTempFile(JNA_TMPLIB_PREFIX, Platform.isWindows()?\".dll\":null, dir);\n                if (!Boolean.getBoolean(\"jnidispatch.preserve\")) {\n                    lib.deleteOnExit();\n                }\n                LOG.log(DEBUG, \"Extracting library to {0}\", lib.getAbsolutePath());\n                fos = new FileOutputStream(lib);\n                int count;\n                byte[] buf = new byte[1024];\n                while ((count = is.read(buf, 0, buf.length)) > 0) {\n                    fos.write(buf, 0, count);","sourceCodeStart":1212,"sourceCodeEnd":1248,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L1212-L1248","documentation":"For non-file URLs (e.g. inside a jar/zip served by a custom classloader), extractFromResourcePath opens the resource stream; if URL.openStream() returns null it throws IOException 'Can't obtain InputStream for <resourcePath>'. This means the URL resolved but its handler cannot deliver the content, so the native library cannot be unpacked.","triggerScenarios":"extractFromResourcePath on a jar:/bundle:/resource: style URL where openStream yields null - typically a nested/bundled JAR (OSGi, one-jar, Spring Boot nested jars) whose handler cannot open the entry.","commonSituations":"Running inside OSGi or application servers where JNA's JAR is embedded in another bundle; Spring Boot executable jars with the default nested URL handler; proxy/security managers blocking stream opening.","solutions":["Unpack JNA so libjnidispatch resources are in a plain JAR/classpath directory rather than a nested JAR","Use framework features that expose nested content (e.g. Spring Boot's repackaged loader compatibility, OSGi Bundle-NativeCode or fragment host for natives)","Install the native library on disk and supply -Djna.boot.library.path to bypass resource extraction","Set -Djna.tmpdir to a writable location and ensure no security policy prevents URL stream opening"],"exampleFix":"// maven-shade: keep jna resources at jar root instead of nested\n// before\n<filter><artifact>net.java.dev.jna:jna</artifact><excludes><exclude>com/sun/jna/**</exclude></excludes></filter>\n// after\n<filter><artifact>net.java.dev.jna:jna</artifact><includes><include>**</include></includes></filter>","handlingStrategy":"try-catch","validationCode":"// probe the stream before relying on extraction\ntry (InputStream is = url.openStream()) {\n    if (is == null) throw new IllegalStateException(\"Cannot open stream for \" + url + \" - nested/bundled jar?\");\n} catch (IOException e) { throw new IllegalStateException(\"Resource unreadable: \" + url, e); }","typeGuard":null,"tryCatchPattern":"try { Native.extractFromResourcePath(res, loader); } catch (IOException e) { if (String.valueOf(e.getMessage()).startsWith(\"Can't obtain InputStream\")) { throw new IllegalStateException(\"Native JAR is nested (OSGi/boot-jar); unpack it or use jna.boot.library.path\", e); } throw e; }","preventionTips":["Keep the JNA JAR as a top-level entry in OSGi/one-jar/Spring Boot layouts","Use bundle fragments or Bundle-NativeCode for OSGi native libraries","Fall back to an on-disk native library via jna.boot.library.path"],"tags":["java","io","classpath","unpacking"],"backgroundTag":"resource-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"}