Error if argument passed to java.exe contains non-ASCII character

Stephanie Crater scrater at microsoft.com
Thu Jun 16 23:53:24 UTC 2022


Hi,



I've been investigating an error on Windows in which compilation fails when the SDK file path includes a Chinese character (and more broadly, if any string argument passed to java.exe contains a non-ASCII character). This happens because command line arguments are read using GetCommandLine() [1]. In the Windows file processenv.h, this resolves to GetCommandLineW [2] if UNICODE is defined and GetCommandLineA [3] otherwise. As UNICODE is not defined, GetCommandLineA is used and Chinese characters on the command line are converted to "?", causing the following:

Compilation failed with an internal error.

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <?> at index 34: C:\Program Files (x86)\Android\SDK????\platforms\android-31\android.jar



This error has been reported before, including JDK-8124977 [4] (describes command line encoding challenges on Windows, created in 2015 and still unresolved) and JDK-7027461 [5] (resolved as a duplicate in 2011, but the problem has actually not been fixed). As described in JDK-7027461, this is a fairly significant issue, especially for Asian users with non-ASCII user names and directory paths.



We have a few options to fix this:

  1.  We can define UNICODE at build time so Windows always chooses GetCommandLineW - would doing this break anything else that I'm not aware of?
  2.  We can choose to call GetCommandLineW directly if deemed appropriate. In my opinion, we should only do this if defining UNICODE isn't a viable option, as it's best practice to use the generic function and let Windows compile it into a Unicode or ANSI function [6].



I'd love to hear the community's thoughts on this, namely if there is any reason we can't define UNICODE or if there are any scenarios where using GetCommandLineA (ANSI) is preferable to GetCommandLineW (Unicode). Or if anyone has any other suggestions for possible solutions, please let me know!



Thanks,

Stephanie



[1] https://urldefense.com/v3/__https://github.com/openjdk/jdk/blob/e833c1d179f926edabb2d40c6c357ab20bba726e/src/java.base/share/native/launcher/main.c*L113__;Iw!!ACWV5N9M2RV99hQ!LLvyWJ5tM1ap1lkjOkP4XSEga_rEssSjz5xBE-eQ8Q0a460wTUGp5c7vBXCIHUN7rCwk_zIXy6QgFr6090Lc$ 

[2] GetCommandLineW function (processenv.h) - Win32 apps | Microsoft Docs<https://urldefense.com/v3/__https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinew__;!!ACWV5N9M2RV99hQ!LLvyWJ5tM1ap1lkjOkP4XSEga_rEssSjz5xBE-eQ8Q0a460wTUGp5c7vBXCIHUN7rCwk_zIXy6QgFkDY0xr_$ >

[3] GetCommandLineA function (processenv.h) - Win32 apps | Microsoft Docs<https://urldefense.com/v3/__https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinea__;!!ACWV5N9M2RV99hQ!LLvyWJ5tM1ap1lkjOkP4XSEga_rEssSjz5xBE-eQ8Q0a460wTUGp5c7vBXCIHUN7rCwk_zIXy6QgFuD1H-56$ >

[4] [JDK-8124977] cmdline encoding challenges on Windows - Java Bug System (openjdk.org)<https://bugs.openjdk.org/browse/JDK-8124977>

[5] [JDK-7027461] Command Line Arguments not Encoded Properly - Java Bug System (openjdk.org)<https://bugs.openjdk.org/browse/JDK-7027461>

[6] Conventions for Function Prototypes - Win32 apps | Microsoft Docs<https://urldefense.com/v3/__https://docs.microsoft.com/en-us/windows/win32/intl/conventions-for-function-prototypes__;!!ACWV5N9M2RV99hQ!LLvyWJ5tM1ap1lkjOkP4XSEga_rEssSjz5xBE-eQ8Q0a460wTUGp5c7vBXCIHUN7rCwk_zIXy6QgFiXV8vwR$ >

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20220616/69e80aa1/attachment.htm>


More information about the jdk-dev mailing list