问题
I am using a Java
library in Clojure
. It does output many things directly to the console using System.out.println
.
I am using the latest Cider with Emacs. I am starting Cider with cider-jack-in
. However, I can see these outputs. I thought they would be copied to the nrepl-server buffer, but it is not.
I also tried to run (alter-var-root #'*out* (constantly *out*))
in the REPL, but no success.
What am I missing in how this should be done, if it can be done at all.
回答1:
You need to replace the java.io.PrintStream
assigned to System.out
with one that will forward everything to clojure.core/*out*
(which is an instance of java.io.PrintWriter
). To do that you have to create an adapter from PrintStream
to PrintWriter
. I haven't found an existing one in the Java SDK API but there is one in Apache Commons IO (or you can implement it by yourself):
(import (java.io PrintStream)
(org.apache.commons.io.output WriterOutputStream))
(-> *out*
(WriterOutputStream.)
(PrintStream.)
(System/setOut))
From now on when you call System.out.println
anywhere in your JVM the output will be passed to clojure.core/*out*
.
来源:https://stackoverflow.com/questions/37414126/output-system-out-println-into-emacs-ciders-repl-or-other-buffer