Created
October 10, 2023 18:53
-
-
Save volgar1x/3b27360eac7b989c167bf7d548135f9c to your computer and use it in GitHub Desktop.
GraalVM CE 21: java.nio.file.InvalidPathException
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ghcr.io/graalvm/graalvm-community:21 AS build | |
WORKDIR /app | |
COPY NioPath.java . | |
RUN javac NioPath.java && native-image -Dsun.jnu.encoding=UTF-8 NioPath | |
FROM debian:12-slim AS production | |
COPY --from=build /app/niopath /bin/niopath | |
CMD ["/bin/niopath", "àéïõû.txt"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.charset.Charset; | |
import java.nio.ByteBuffer; | |
import java.io.File; | |
public class NioPath { | |
public static void main(String[] args) { | |
test("UTF-8", args[0]); | |
test(System.getProperty("file.encoding"), args[0]); | |
test(System.getProperty("sun.jnu.encoding"), args[0]); | |
test(System.getProperty("sun.jnu.encoding"), "àéïõûy.txt"); | |
// throws java.nio.file.InvalidPathException | |
try { System.out.println(Paths.get(args[0])); } | |
catch (Throwable th) { System.out.println(th); } | |
// ok | |
try { System.out.println(new File(args[0])); } | |
catch (Throwable th) { System.out.println(th); } | |
// throws java.nio.file.InvalidPathException | |
try { System.out.println(Paths.get(new File(args[0]).toURI())); } | |
catch (Throwable th) { System.out.println(th); } | |
// throws java.nio.file.InvalidPathException | |
try { System.out.println(new File(args[0]).toPath()); } | |
catch (Throwable th) { System.out.println(th); } | |
} | |
private static void test(String charsetName, String value) { | |
System.out.println("testing with charset: " + charsetName); | |
Charset charset = Charset.forName(charsetName); | |
ByteBuffer encoded = charset.encode(value); | |
System.out.print("encoded: "); | |
while (encoded.hasRemaining()) { | |
System.out.print(Integer.toHexString(encoded.get() & 0xff)); | |
} | |
System.out.print("\n"); | |
} | |
} |
You need to give an encoding during compilation of the image instead of providing it afterward
Ignoring the fact that this command is generated, it does look correct to me:
native-image --no-fallback -H:+StaticExecutableWithDynamicLibC -march=compatibility -H:+AddAllCharsets -Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 -H:Name=/layers/paketo-buildpacks_native-image/native-image/com.github.schaka.janitorr.JanitorrApplicationKt -cp /workspace:/workspace/BOOT-INF/classes:/workspace/BOOT-INF/lib/feign-jackson-13.1.jar:/workspace/BOOT-INF/lib/spring-boot-actuator-autoconfigure-3.4.0-M3.jar:/workspace/BOOT-INF/lib/jackson-datatype-jdk8-2.17.2.jar:/workspace/BOOT-INF/lib/jackson-datatype-jsr310-2.17.2.jar:/workspace/BOOT-INF/lib/jackson-module-parameter-names-2.17.2.jar:/workspace/BOOT-INF/lib/jackson-databind-2.17.2.jar:/workspace/BOOT-INF/lib/jackson-annotations-2.17.2.jar:/workspace/BOOT-INF/lib/jackson-core-2.17.2.jar:/workspace/BOOT-INF/lib/jackson-module-kotlin-2.17.2.jar:/workspace/BOOT-INF/lib/kotlin-reflect-2.0.20.jar:/workspace/BOOT-INF/lib/kotlinx-coroutines-core-jvm-1.8.1.jar:/workspace/BOOT-INF/lib/kotlin-stdlib-2.0.20.jar:/workspace/BOOT-INF/lib/caffeine-3.1.8.jar:/workspace/BOOT-INF/lib/feign-httpclient-13.1.jar:/workspace/BOOT-INF/lib/feign-core-13.1.jar:/workspace/BOOT-INF/lib/jcl-over-slf4j-2.0.16.jar:/workspace/BOOT-INF/lib/annotations-23.0.0.jar:/workspace/BOOT-INF/lib/spring-webmvc-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/spring-web-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/spring-context-support-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/micrometer-jakarta9-1.14.0-M3.jar:/workspace/BOOT-INF/lib/spring-boot-autoconfigure-3.4.0-M3.jar:/workspace/BOOT-INF/lib/spring-boot-actuator-3.4.0-M3.jar:/workspace/BOOT-INF/lib/spring-boot-3.4.0-M3.jar:/workspace/BOOT-INF/lib/spring-context-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/micrometer-core-1.14.0-M3.jar:/workspace/BOOT-INF/lib/micrometer-observation-1.14.0-M3.jar:/workspace/BOOT-INF/lib/checker-qual-3.37.0.jar:/workspace/BOOT-INF/lib/error_prone_annotations-2.21.1.jar:/workspace/BOOT-INF/lib/httpclient-4.5.14.jar:/workspace/BOOT-INF/lib/logback-classic-1.5.8.jar:/workspace/BOOT-INF/lib/log4j-to-slf4j-2.24.0.jar:/workspace/BOOT-INF/lib/jul-to-slf4j-2.0.16.jar:/workspace/BOOT-INF/lib/slf4j-api-2.0.16.jar:/workspace/BOOT-INF/lib/jakarta.annotation-api-2.1.1.jar:/workspace/BOOT-INF/lib/spring-aop-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/spring-beans-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/spring-expression-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/spring-core-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/snakeyaml-2.3.jar:/workspace/BOOT-INF/lib/tomcat-embed-websocket-10.1.30.jar:/workspace/BOOT-INF/lib/tomcat-embed-core-10.1.30.jar:/workspace/BOOT-INF/lib/tomcat-embed-el-10.1.30.jar:/workspace/BOOT-INF/lib/micrometer-commons-1.14.0-M3.jar:/workspace/BOOT-INF/lib/httpcore-4.4.16.jar:/workspace/BOOT-INF/lib/commons-logging-1.2.jar:/workspace/BOOT-INF/lib/commons-codec-1.17.1.jar:/workspace/BOOT-INF/lib/spring-jcl-6.2.0-RC1.jar:/workspace/BOOT-INF/lib/HdrHistogram-2.2.2.jar:/workspace/BOOT-INF/lib/LatencyUtils-2.0.3.jar:/workspace/BOOT-INF/lib/logback-core-1.5.8.jar:/workspace/BOOT-INF/lib/log4j-api-2.24.0.jar com.github.schaka.janitorr.JanitorrApplicationKt
Is there really no solution for changing the default encoding of a native image to UTF-8 in such a way that Path.of
works?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I just encountered this problem. Did you ever find a solution for GraalVM native images properly accepting encoding?
No matter what I pass to my container, it seems
sun.jnu.encoding
is always POSIX