使用java -jar调用时为什么打印功能不打印?

时间:2016-08-14 14:27:17

标签: java jar clojure leiningen

我用 leiningen 学习 clojure 。我编写了一个简单的代码来测试 lein & java -jar 命令。这是我的 project.clj 文件:

$ cat project.clj 
(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :main ^:skip-aot hello.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

这是我的源代码:

$ cat src/hello/core.clj 
(ns hello.core
  (:gen-class))

(defn -main
  [& args]
  (print "Hello, World!"))

当我使用 lein 运行此代码时,它运行正常。它显示在这里

$ lein run
Hello, World!$

当我尝试运行 java -jar 时,它没有工作

$ lein uberjar
Compiling hello.core
Created /home/rishi/hello/target/uberjar/hello-0.1.0-SNAPSHOT.jar
Created /home/rishi/hello/target/uberjar/hello-0.1.0-SNAPSHOT-standalone.jar

$ java -jar target/uberjar/hello-0.1.0-SNAPSHOT-standalone.jar

我不明白,为什么我没有用 java -jar 获得所需的输出?

如果我在源文件中将打印替换为 println ,我的 lein run &amp ;; java -jar

1 个答案:

答案 0 :(得分:4)

您的流程输出已缓冲。 print不会刷新输出。大多数情况下,您的输出流将设置为自动刷新换行符,这就是显示println的原因。如果您需要在没有换行符的情况下进行刷新,则可以调用(flush)函数。