3

Here is my code:

Process p = Runtime.getRuntime().exec(new String[]{"bash","-c",new String(command.getBytes(),"utf-8")});

I found out that there is no use of new String(command.getBytes(),"utf-8").

How can I to set charset?

My app is a spring boot application.

The detail command is

./xxx.jar --execute "select * from xxx where a = `我`" 

When I execute the command directly in the shell, it runs well, but the java code gets garbled.

I set -Dfile.encoding=UTF-8,but it is no use for me. Why?

Baby.zhou
  • 521
  • 1
  • 4
  • 21
  • 1
    Does this here https://perlgeek.de/en/article/set-up-a-clean-utf8-environment help in any way? Meaning: you sure that the bash side of things works as you expect it to? – GhostCat Dec 12 '16 at 09:31
  • @GhostCat,I run the app in `docker`,the command runs well by shell directly,but code garbled. – Baby.zhou Dec 12 '16 at 09:41
  • 1
    Possible duplicate of [Setting the default Java character encoding?](http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding) – Serg M Ten Dec 12 '16 at 09:46
  • Just in case you don't get a good answer here; there would be a simple workaround: you could write the SQL statement into some file (which should definitely work with UTF-8) and have your application read that file; instead of passing stuff directly. But of course, that is more like a dirty little hack. – GhostCat Dec 12 '16 at 09:46
  • @Baby.zhou set locale variable in shell environment correctly. – vahid Dec 12 '16 at 09:50
  • @SergioMontoro,I set `-Dfile.encoding=UTF-8`,but it is no use. – Baby.zhou Dec 12 '16 at 10:19
  • You may try to call [locale](https://www.cyberciti.biz/faq/how-to-set-locales-i18n-on-a-linux-unix/) from exec() but since the problem is within the string of your SQL query I think you'd better encode the string with escaped character codes like "\\uXXXX" discussion [here](http://stackoverflow.com/questions/13287145/mysql-querying-for-unicode-entities) Also more info about encoding SQL at [OWASP](https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet) – Serg M Ten Dec 12 '16 at 14:10

0 Answers0