Search code examples
c++utf-8java-native-interfaceextended-ascii

how to pass Extended ASCII characters through JNI via C++


I am trying to use JNI via VC++ and pass a custom directory in the classpath as a UTF-8 string -Djava.class.path=c:\myDir\my.jar

This works well and I could find the methods in the jar using FindClass

But when the directory name contains any extended ASCII characters then FindClass returns NoClassDefFound exception. I guess this is becasue the JVM is not able to load the jar file and I am doing something wrong in passing the arguments.

Here is what I do 1) Create a std::wstring jarPath(L"-Djava.class.path=C:\ÆËñœ\my.jar"); 2) Convert the wstring to CP_UTF8 using WideCharToMultiByte 3) copy the utf8String to optionString and call JNI_CreateJavaJVM

I have tried printing the convertyed utf8 to a log file just before calling the JNI_CReateJavaJVM and it prints perfectly.

Could somebody please tell if the CP_UTF8 (defined in windows SDK WinN;s.h) is not what the JNI expects? How do I debug this problem, any help is greatly appreciated.

Thanks in advance Rahul


Solution

  • The documentation suggests that the option string must be in "the default platform encoding". That probably means you'd need to use CP_ACP. If your characters can't be represented in that encoding then I'm not sure there's much you can do.

    (Would the file's short name be usable instead?)