{"record":{"id":"826ff9c18675bd9c","repo":"java-native-access/jna","slug":"utf-16le-charset-is-not-supported","errorCode":null,"errorMessage":"UTF-16LE charset is not supported","messagePattern":"UTF-16LE charset is not supported","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"contrib/platform/src/com/sun/jna/platform/win32/WTypes.java","lineNumber":148,"sourceCode":"        /**\n         * @deprecated Users should not change the value of an allocated {@link BSTR}.\n         */\n        @Deprecated\n        public void setValue(String value) {\n            if(value == null) {\n                value = \"\";\n            }\n            try {\n                byte[] encodedValue = value.getBytes(\"UTF-16LE\");\n                // 4 bytes for the length prefix, length for the encoded data,\n                // 2 bytes for the two NULL terminators\n                Memory mem = new Memory(4 + encodedValue.length + 2);\n                mem.clear();\n                mem.setInt(0, encodedValue.length);\n                mem.write(4, encodedValue, 0, encodedValue.length);\n                this.setPointer(mem.share(4));\n            } catch (UnsupportedEncodingException ex) {\n                throw new RuntimeException(\"UTF-16LE charset is not supported\", ex);\n            }\n        }\n\n        public String getValue() {\n            try {\n                Pointer pointer = this.getPointer();\n                if(pointer == null) {\n                    return \"\";\n                }\n                int stringLength = pointer.getInt(-4);\n                return new String(pointer.getByteArray(0, stringLength), \"UTF-16LE\");\n            } catch (UnsupportedEncodingException ex) {\n                throw new RuntimeException(\"UTF-16LE charset is not supported\", ex);\n            }\n        }\n\n        @Override\n        public String toString() {","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/WTypes.java#L130-L166","documentation":"WTypes.BSTR.setValue() encodes the Java string as UTF-16LE via String.getBytes(\"UTF-16LE\"). Every compliant JVM must provide this charset, but if the encoding lookup throws UnsupportedEncodingException the library wraps it in this RuntimeException. It indicates a fundamentally broken JVM charset configuration rather than a data problem.","triggerScenarios":"Constructing a BSTR (or calling setValue) on a JVM whose charset provider fails to resolve \"UTF-16LE\" — essentially only with a broken/exotic JRE, a faulty CharsetProvider, or a severely corrupted Java installation.","commonSituations":"Running on a stripped-down/custom JVM build lacking standard charsets; broken java.nio.charset.spi.CharsetProvider registration on the classpath; corrupted JRE runtime files.","solutions":["Use a standard, up-to-date JVM (Temurin/Oracle/Amazon Corretto) instead of a stripped custom runtime.","Check for custom CharsetProvider implementations on the classpath and remove/fix them.","Reinstall/verify the JDK/JRE installation.","Catch the RuntimeException only as a last resort — it signals an unrecoverable environment problem."],"exampleFix":"// before\nBSTR bstr = new BSTR(\"value\"); // throws on broken JVM\n// after\nif (Charset.isSupported(\"UTF-16LE\")) {\n    BSTR bstr = new BSTR(\"value\");\n} else {\n    throw new IllegalStateException(\"JVM missing UTF-16LE charset\");\n}","handlingStrategy":"type-guard","validationCode":"if (!Charset.isSupported(\"UTF-16LE\"))\n    throw new IllegalStateException(\"JVM lacks UTF-16LE charset; use a standard JRE\");","typeGuard":"static boolean utf16leSupported() {\n    try { return Charset.forName(\"UTF-16LE\") != null; }\n    catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n    BSTR b = new BSTR(value);\n} catch (RuntimeException e) {\n    throw new IllegalStateException(\"Broken JVM charset environment\", e);\n}","preventionTips":["Use standard, current JVM distributions.","Do not ship custom CharsetProvider SPIs unless necessary.","Verify charsets in a startup self-check."],"tags":["windows","jna","charset","bstr"],"backgroundTag":"unsupported-operation","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}