3

I'm working with Clojure code in Windows.

If I directly use Clojure's jar file, it outputs a non-ASCII (Japanese) string without problems.

hello.clj:

(println "こんにちは")

> java -jar 1.5.1.jar hello.clj
こんにちは

But in the case of a Leiningen project, it doesn't output the characters as expected.

src/hello/core.clj:

(defn -main [& args]
  (println "こんにちは"))

> lein run
????????

In both cases, the encoding of the source files is UTF-8, the class of *out* is java.io.OutputStreamWriter, and its encoding is MS932 (code page for Japanese).

(println "*out* :" (class *out*) (.getEncoding *out*))
;; *out* : java.io.OutputStreamWriter MS932

I know it will work correctly if I set the environment variable JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 and use a terminal that supports UTF-8.

But I want to output with MS932 in Windows' default console, as in the jar file case.

Community
  • 1
  • 1
yohshiy
  • 66
  • 5

1 Answers1

0

This is an interesting problem... it seems strange that running (println *out* (class *out*) (.getEncoding *out*)) using Leiningen would give you the same results as running it directly using the Clojure jar file, if one of them returns こんにちは and the other returns ????????. It seems like there is an issue with Leiningen's default encoding in Windows, but I would think that running (.getEncoding *out*) from Leiningen in your environment would give you something other than MS932, otherwise I don't see why it wouldn't print Japanese characters in a Windows terminal...

There have been issues before with Leiningen preview builds and character encoding doing unexpected things... see this issue from 2012 -- although it may have been OS X specific (and a much older version of Leiningen), the issue was that JVM's default encoding was not set to UTF-8. I believe UTF-8 is the default now, but maybe in some Windows environments you need MS932 instead. I don't use Windows, so this is just a shot in the dark, but...

It would appear that the environment variable LEIN_JVM_OPTS needs to be set to -Dfile.encoding=MS932. See this previous SO question about setting Leiningen JVM options on a per-project basis. Try adding this line to your project.clj:

:jvm-opts ["-Dfile.encoding" "MS932"]

If that works, then you might consider adding it under to your :user entry in profiles.clj, i.e.:

{:user {:jvm-opts ["-Dfile.encoding" "MS932"]}}
Community
  • 1
  • 1
Dave Yarwood
  • 2,866
  • 1
  • 17
  • 29
  • Thank you for your answer. I tried them, but the results was unchanged. I added `:jvm-opts ["-Dfile.encoding" "MS932"]` to a project.clj, but that didn't work by an error. I wondered `:jvm-opts ["-Dfile.encoding=MS932"]`. I modified the project.clj, and tried again. the result of output stay "????????". So I set directly the environment variable `LEIN_JVM_OPTS=-Dfile.encoding=MS932`, but the result was same. – yohshiy Aug 28 '14 at 17:03