0

I've been writing a program on Java with library jacob, which help to work with wmi. and faced with such a problem, the team does not work in Cyrillic.

   String userName = Dispatch.get(dItem2, "Name").getString();
   String objUser = "WinNT://IUMAG/" + userName + ",user";
   Dispatch dServ2 = new Dispatch(objUser);

If the username is written in Russian, then there is an error, and if English, then everything is fine How can I solve this problem?

AsmaRod
  • 101
  • 2
  • 1
    Check you encoding... UTF-8 supports does Cyrilic (and whatever other text you got). Windows default is CP1251 /cp1252?... You can try to translate the text to byte [] , then back to `new String(byteArray, "UTF-8")` when you print this, you should see Cyrillic – Danielson Jul 29 '15 at 12:07
  • I tried, did not work :( – AsmaRod Jul 29 '15 at 12:37
  • Ok... First, what is the error? And what are the characters you get? (Sorry for the text, I don't know what the following means) `Кириллон алфавит` or `ÐиÑиллон алÑавиÑ`? Or do you get the `�` Unicode unknown character? (translated what I wrote, seems not to be offensive, nice) – Danielson Jul 29 '15 at 12:44
  • > Exception in thread "main" com.jacob.com.ComFailException: Can't find moniker at com.jacob.com.Dispatch.createInstanceNative(Native Method) at com.jacob.com.Dispatch.(Dispatch.java:99) at javaapplication20.TestMain.main(TestMain.java:43) Java Result: 1 I use Russian language, example: "Администратор" – AsmaRod Jul 29 '15 at 13:18

1 Answers1

0

I think the answer goes to (for English speakers) from http://www.sql.ru/forum/1167615/java-jacob-rabota-s-kirillicey:

Macro A2W converts data from ANSI encoding in Windows Wide char. Hence the conclusion, the input line should contain data in ANSI encoding Windows (probably in 1251), but not in UTF8 (as is customary in Java). Authors JACOB course little monsters that have not made ​​the conversion from UTF8 (adopted in Java) in Wide char. But I think the national names in COM not so common. including I would like to try a hand in Java to convert a string of UTF in 1251. It is clear that work is only on Windows, where as ANSI encoding is 1251, ie Default characterset = Russian

Originally in Russian:

Макрос A2W преобразует данные из ANSI кодировки Windows в Wide char. Отсюда вывод, входные строка должна содержать данные в ANSI кодировки Windows (скорее всего 1251), а НЕ в UTF8 (как принято в Java).

Авторы JACOB конечно немного уроды, что НЕ сделали преобразование из UTF8 (принятой в Java) в Wide char. Но, думаю, национальные имена в COM не так часто встречаются.

Т.ч. я бы попробывал просто руками в Java преобразовать строку из UTF в 1251. Понятное дело, что работать будет только на Windows, где в качестве ANSI кодировки стоит 1251, т.е. Default characterset = Russian.

So, Java, by default, uses UTF-8, but Windows, hence the Jakob, uses cp1251. So convert the username, if Character.UnicodeBlock.CYRILLIC then convert from UTF-8 to cp1251.

Disclaimer, I'm unsure how good the Google translators translated the page...

Update, using UnicodeBlock to determine character type, see http://docs.oracle.com/javase/7/docs/api/java/lang/Character.UnicodeBlock.html for more information on UnicodeBlocks

//XXX over simplification, for it can also be CJK (Chinese Japanese Korean) or other...

public String hasCyrillicCharacters(final String text){
    final char[] cc = text.toCharArray();
    for (char c : cc) {
        if (UnicodeBlock.CYRILLIC == UnicodeBlock.of(c)) {
            return true;
        }
    }
    return false;
}
Danielson
  • 2,605
  • 2
  • 28
  • 51
  • Character.UnicodeBlock.CYRILLIC What it? Tell me how to use it, please? – AsmaRod Jul 29 '15 at 15:17
  • See last part of updated answer... If you wish to use other character sets (note I'm unsure about the cp1251 mapping!) you can test other UnicodeBlock – Danielson Jul 29 '15 at 15:25
  • I assume you have tried searching in Russian? My Russian is poor, at best. But this problem will most probably be document (if more than the one I proposed) in Russian, or Chinese maybe? – Danielson Jul 29 '15 at 16:18
  • @AsmaRod btw (maybe I assumed incorrectly) but you might try in the Russian StackOverFlow http://ru.stackoverflow.com/ if they speak the language, and code... problems (good ones :) ) – Danielson Jul 29 '15 at 16:20
  • One more question, please. Did I change utf to windows-125, right? – AsmaRod Jul 31 '15 at 12:42
  • String objUser = "WinNT://IUMAG/Администратор,user"; String objUser2 = new String(objUser.getBytes("utf-8"),"cp1251"); – AsmaRod Jul 31 '15 at 12:42
  • I think: String objUser3 = new String(objUser.getBytes("utf-8"), "Windows-1252"); -> WinNT://IUMAG/Ð�дминиÑ�тратор,user instead of cp1251 -> WinNT://IUMAG/Администратор,user – Danielson Jul 31 '15 at 12:54